Every variable or constant (simple or complex) has built-in functions. They can be accessed like any other function even on a literal. Every built-in function can be overriden.
nameof
nameof returns the name of a variable as a string. It can be used both with and without a parameter.
With parameter
Example
var a =42nameof(a) //returns 'a'; the nameof function of the `this` scope is called
Without parameter
Example
with console from std:ioconsole.nameof() //returns 'console'
type
type returns the type of an object as an atom.
Example
var a =42var b ="Hello world"var c =truea.type() //returns :intb.type() //returns :stringc.type() //returns :bool:ok.type() //returns :atom
equals
equals compares two values and returns true if they're equal and false if they're not.
Example
with list from std:collections1.equals(2) //returns false"Hello".equals("Hello") //returns truevar foo =list.of({1,2,3,4,5})foo.equals(list.of({1,2,3,4,5})) //returns true
hash
For complex types (except protos), hash serializes the variable into a JSON, MD5-hashes the JSON string and returns it. For simple types, the MD5-hash of the string representation of the value is returned.
Example
var jd =User("John","Doe")jd.hash() //returns the MD5 of the JSONified User object"Hello".hash() //returns the MD5 hash of "Hello"9000.hash() //returns the MD5 hash of "9000"
toString
toString returns the string representation of any variable or literal. Objects or structs are serialized into JSON while simple types are just being converted to strings.
Example
with list from std:collectionslist.of({1,2,3,4,5}).toString()/*returns:[1,2,3,4,5]*/classCar @publicvarnamevarmodelsendendclassPersonletnamestringvarageintvarcarsobject::Car[]endPerson("John",30,{Car("Ford",{"Fiesta", "Focus", "Mustang"}),Car("Fiat",{"500", "Panda"})}).toString()/*returns:{ "name":"John", "age":30, "cars": [ { "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] }, { "name":"Fiat", "models":[ "500", "Panda" ] } ] }*/
send & self
send sends an object to the specified PID.
self returns the pid of the calling process.
Example
with msg from std:ioclassCounter private var int counter func loop() receive(m) msg{state::increment} => counter++msg{state::get, data := sender} =>send(sender, counter) end endfuncCounter() counter =0 endendvar ctr =spawn({_=> Counter().loop()})send(ctr, msg(:increment))send(ctr, msg(:increment))send(ctr, msg(:increment))send(ctr, msg(:get, self()))receive(m)_=> console.out(m)//prints 3end