HadesLang Doc
Search…
Type conversions in simple variable types
Every simple variable type supports type conversion by invoking the constructor of the type.

int

Available for uint8, uint16, uint32/uint, uint64, int8, int16, int32/int and int64. When the conversion to an int fails, an exception is thrown.

Example

1
int("22") //is being converted to 22
2
int(1.5) //exception is thrown
3
int(true) //is being converted to 1
Copied!

string

The string constructor accepts every type except for protos. Objects and structs are converted into JSON, other simple types are just being converted into strings (like with toString).

Example

1
string(21) //returns "21"
2
string(true) //returns "true"
3
string(22.3) //returns "true"
Copied!

float

Available for float32/float as well as float64/double. When the conversion to a float fails, an exception is thrown.

Example

1
float("22") //returns 22.0
2
float("1.5") //returns 1.5
3
float(false) //returns 0.0
4
float(10) //returns 10.0
Copied!

bool

When the conversion to a bool fails, an exception is thrown.

Example

1
bool(1) //returns true
2
bool("false") //returns false
3
bool(0.0) //returns false
4
bool("not a bool") //raises TypeConversionException
Copied!

atom

Under the hood, the conversion to an atom will use toString. The conversion to an atom will only fail if one tries to convert a non-simple datatype.

Example

1
atom(1) //returns :1
2
atom(true) //returns :true
3
atom("ok") //returns :ok
Copied!

pid

The only datatype that can be converted to a pid is an int. That is because the value of a pid is an int.
Last modified 1yr ago