donotturnoff

cast

cast provides support for type casting in Rex.


Casting functions

str

& str type -> str
str x = str(x)

This casts a value of any type into a string using Python's str function.

int

& int type -> int
int x = int(x)

This casts a value of any type into an integer using Python's int function. On failure, it will throw a typeErr.

num

& num type -> num
num x = float(x)

This casts a value of any type into a number using Python's float function. On failure, it will throw a typeErr.

float

& float type -> float
float x = float(x)

This casts a value of any type into a floating-point number using Python's float function. On failure, it will throw a typeErr.

whole

& whole type -> whole
whole x =
    | int(x) if >= int x 0
    | error typeErr . . "Cannot cast " x " to a whole number" otherwise

This casts a value of any type into a whole number using Python's int function. If the argument cannot be cast to an integer or if it is negative after being cast to an integer, a typeErr is thrown.

natural

& natural type -> natural
natural x ==
    | int(x) if > int x 0
    | error typeErr . . "Cannot cast " x " to a natural number" otherwise

This casts a value of any type into a natural number using Python's int function. If the argument cannot be cast to an integer or if it is negative or zero after being cast to an integer, a typeErr is thrown.

bool

& bool type -> bool
bool x ==
    | bool(x) if || || || == x "True" == x "False" == x 0 == x 1
    | error typeErr . . "Cannot cast " x " to a Boolean" otherwise

This casts a value of any type into a Boolean using Python's bool function. It will perform the cast if the argument is "True", "False", 0 or 1, otherwise it will throw a typeErr.