Skip to content

09. Type Casts

Tom Dodd edited this page Dec 8, 2023 · 1 revision

Some types can be cast to other types, which means that values with particular types can be transformed into values of different types. Any keyword requiring a value of a particular type will attempt an implicit cast on the value it pops from the stack. The print and println keywords are exceptions to this rule, as they will explicitly cast their arguments to a string, which is valid for all elements. If an implicit cast fails, it may cause the action of the keyword to throw an error.

Casts can also be made by using the type and cast keywords: the former pops an element and pushes its class, while the latter attempts an explicit cast of its first argument to the type defined by the second argument. A failed explicit cast will immediately throw an error.

Implicit casts are more conservative than explicit ones, and apart from very particular cases such as integers to floats, they are defined as null if the element is not an instance of the type being cast to.

Clone this wiki locally