Type Casts
Sometimes it is useful to directly cast a type to another one. Spice offers type casting for some type combinations. Additionally, the casting operator can always be applied when the source type matches the cast destination type.
Usage¶
Casting an int to the short data type:
Example for casting for a function call:
| Spice | |
|---|---|
Pointer casts¶
Casting between two pointer types, and between a pointer and an integer, is only allowed inside an unsafe
block, because the compiler cannot check that the result is still a valid pointer.
On the integer side, only long (and unsigned long) is accepted. It is the only Spice integer type that is
guaranteed to be 64 bit wide on every target, so it is the only one that cannot silently truncate a pointer:
| Spice | |
|---|---|
Casting to a narrower integer type such as int or short is rejected:
| Spice | |
|---|---|
Since Spice has no pointer-difference operator, going through long is also how you compute the numeric
distance between two pointers, in bytes: