Built-in Functions
INPUT
Standard input can be read from with ???
.
It will read until it receives a newline character.
x: ???;
will assign to x
as a string what it reads from standard input,
stopping at a newline character.
A prompt can be given by preceding the ???
with a string,
for example x: "input: "???;
.
Related library:
io
PRINTLN
Objects can be written to standard output by adding an !
at the end.
Note that they won't be written exactly as they would appear in Samarium.
"a"!
will write a
to standard output.
//\/!
will write 13
to standard output.
This function will return what it writes to stdout (though not necessarily as a string), and can thus be used in an assignment statement for example.
x: //\! == optional semicolon after !
== the string "6" is written to stdout, and `x` now has the value 6 (number)
THROW
Similarly to PRINTLN, some objects can be written to standard error using !!!
.
This will throw an error, and exit the program if the error is not caught.
Only strings and arrays of length 2 can be used for throwing errors.
"exception raised"!!! == optional semicolon after !!!
== the following is written to stderr:
== [Error] exception raised
== 2-element arrays can be used for adding notes to exceptions:
["invalid date format", "use YYYY-MM-DD format"]!!!
== [Error] invalid date format
== [Note] use YYYY-MM-DD format
EXIT
The program may be exited with =>!
.
If a particular exit code is desired, it may be put after the exclamation mark:
=>! //
will exit the program with exit code 3.
A string may also be supplied instead of a number, which will first write it to stderr and then exit the program with exit code 1.
=>! "uh oh";
is equivalent to"uh oh\n" ~> /\; =>! /;
HASH
The hash function ##
returns as a integer the hash value of an object if it
has one (arrays and tables do not). The value it returns will remain consistent
for the life of the program, but may vary if the program is run multiple times.
"hash"##
will return the hash value of the string "hash"
.
TYPEOF
The typeof function ?!
returns the type of an object,
as an instance of the Type
class.
/?!
returnsNumber
.
"abc"?!
returnsString
.
/?!?!
returnsType
.
These instances are callable and can be used to convert values into that type, like so:
\?!("123") + /\!;
== writes `125` to stdout
Related library:
types
SPECIAL
The special method $
has different uses depending on the type of object it's
used on.
Object | Use |
---|---|
Array | Returns the length of the array |
Function | Returns the number of parameters the function has |
Number | Returns the integer part of the number as a string |
Slice | Returns the length of the range covered by the slice |
String | Returns the length of the string |
Table | Returns an array of the table's values |
For example:
"string"$!;
== writes `6` to stdout
TIMESTAMP
The timestamp function @@
returns the time in milliseconds since the epoch.
Note
Timezone is included. For UTC timestamp, use datetime.timestamp_utc
.
Related library:
datetime
DTNOW
The dtnow function @@@
gets the system's current date and time as an array of
integers, in the format [year, month, day, hour, minute, second, millisecond,
utc_hour_offset, utc_minute_offset]
.
Related library:
datetime
SLEEP
The sleep function ,.,
pauses execution for the specified number of
milliseconds.
,., /////\/\\\;
== sleep for 1000 milliseconds (1 second)
Related library:
datetime
ASSERT
The assert function !!
is used as a debugging tool.
If the input to this function is falsy (i.e. empty iterable, null, or just
false), an AssertionError
will be raised, otherwise, nothing will happen.
A custom error message can be provided by putting it after a ,
.
!! / > /\, "error message";
will raise [AssertionError] error message
.
PARENT
The parent function !?
gets the parent/inherited classes of the input class.
If a class only has one parent, it will be returned directly.
If a class has multiple parents, they will be returned in an array, in the same
order as in the class definition.
Note that this will only go one layer deep, i.e. if class A
has parent B
and class B
has parent C
, A!?
will only return B
, and A!?!?
will
return C
.
RANDOM
The special method ??
has different uses depending on the type of object it's
used on.
Object | Use |
---|---|
Array | Returns a random element |
Number | Returns a random number from an interval based on its value n = 0 → 0 n > 0 → [0, n) n < 0 → [n, 0) If n is an integer, n?? is also going to return integers. |
Slice | Returns a random number in a range defined by that slice |
String | Returns a random character |
Table | Returns a random key |
Related library:
random
ID
The special method **
returns the memory address of a given object.
x: //;
x**!; == 7f7ea0367800