HadesLang Doc
Search…
Built-in functions
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

1
var a = 42
2
nameof(a) //returns 'a'; the nameof function of the `this` scope is called
Copied!

Without parameter

Example

1
with console from std:io
2
console.nameof() //returns 'console'
Copied!

type

type returns the type of an object as an atom.

Example

1
var a = 42
2
var b = "Hello world"
3
var c = true
4
5
a.type() //returns :int
6
b.type() //returns :string
7
c.type() //returns :bool
8
:ok.type() //returns :atom
Copied!

equals

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

Example

1
with list from std:collections
2
3
1.equals(2) //returns false
4
"Hello".equals("Hello") //returns true
5
6
var foo = list.of({1,2,3,4,5})
7
foo.equals(list.of({1,2,3,4,5})) //returns true
Copied!

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

1
var jd = User("John", "Doe")
2
3
jd.hash() //returns the MD5 of the JSONified User object
4
5
"Hello".hash() //returns the MD5 hash of "Hello"
6
7
9000.hash() //returns the MD5 hash of "9000"
Copied!

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

1
with list from std:collections
2
3
list.of({1,2,3,4,5}).toString()
4
/*
5
returns:
6
[1,2,3,4,5]
7
*/
8
9
class Car
10
@public
11
var name
12
var models
13
end
14
end
15
16
class Person
17
let name string
18
var age int
19
var cars object::Car[]
20
end
21
22
Person("John",30,
23
{
24
Car("Ford",{"Fiesta", "Focus", "Mustang"}),
25
Car("Fiat",{"500", "Panda"})
26
}).toString()
27
28
/*
29
returns:
30
{
31
"name":"John",
32
"age":30,
33
"cars": [
34
{ "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] },
35
{ "name":"Fiat", "models":[ "500", "Panda" ] }
36
]
37
}
38
*/
Copied!

send & self

send sends an object to the specified PID.
self returns the pid of the calling process.

Example

1
with msg from std:io
2
3
class Counter
4
private var int counter
5
6
func loop()
7
receive(m)
8
msg{state: :increment} => counter++
9
msg{state: :get, data := sender} => send(sender, counter)
10
end
11
end
12
13
func Counter()
14
counter = 0
15
end
16
end
17
18
var ctr = spawn({_ => Counter().loop()})
19
send(ctr, msg(:increment))
20
send(ctr, msg(:increment))
21
send(ctr, msg(:increment))
22
23
send(ctr, msg(:get, self()))
24
25
receive(m)
26
_ => console.out(m) //prints 3
27
end
Copied!
Last modified 1yr ago