Built-in functions

Last updated 3 months ago

Every variable or constant (simple or complex) have 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 = 42
nameof(a) //returns 'a'; the nameof function of the `this` scope is called

Without parameter

Example

with console from std:io
console->nameof() //returns 'console'

type

type returns the type of an object as a string.

Example

var a = 42
var b = "Hello world"
var c = true
a->type() //returns 'int'
b->type() //returns 'string'
c->type() //returns 'bool'

equals

equals compares two values and returns true if they're equal and false if they're not.

Example

with list from std:collections
1->equals(2) //returns false
"Hello"->equals("Hello") //returns true
var 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:collections
list->of({1,2,3,4,5})->toString()
/*
returns:
[1,2,3,4,5]
*/
class Car
@public
var name
var models
end
end
class Person
let string name
var int age
var object[] cars
end
Person("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" ] }
]
}
*/