Prev | Index | Next |
Explicit type casting is discouraged as much as possible, but is sometimes necessary, especially in the current preliminary version of C*. Casts are written with the "cast" keyword followed by the type in square brackets:
int x := cast[int](1.0);
C-style casts are removed from C*, primarily because they are difficult to parse. In future versions, with the addition of features like typecases and virtualization, it should be easy to avoid most or all explicit casts.
C++-style constructor casts are allowed in C*:
int x := int(1.0);
User-defined Casts
Users can define implicit casts between primitive types. Currently, this is primarly useful to "configure" C* to act like unsafe C/C++, for programmers who prefer brevity to readability. For example, the following definition lets boolean values be treated as integers:
operator int(bool b) { return if (b) 1 else 0; }
The general form is to define an operator whose name is the target type of the cast, and whose parameter is the type being cast. A return type is not needed.
Prev | Index | Next |