The compiler owns a fixed set of traits that let user types work with cast.
String conversion
Label = |
text String,
|
to_string |this Label| -> String:
return this.text
;
Label must CASTABLE_TO_STRING
value String = cast Label("docs")
CASTABLE_TO_STRING means: this type can become a String without failing.
Fallible conversion
Label = |
text String,
|
try_to_int |this Label| -> Int, Error!:
return! Error("not a number")
;
Label must TRY_CASTABLE_TO_INT
number Int = cast Label("42") catch |err|:
then 0
;
TRY_CASTABLE_TO_INT means: this type may become an Int, but it can also fail. The method signals failure with return! and the caller recovers with cast ... catch.
The compiler owns a closed set of static cast traits. They provide source evidence for explicit cast and cast! conversions.
Trait table
Each trait requires one receiver method:
CASTABLE_TO_INT requires to_int |This| -> IntTRY_CASTABLE_TO_INT requires try_to_int |This| -> Int, Error!CASTABLE_TO_FLOAT requires to_float |This| -> FloatTRY_CASTABLE_TO_FLOAT requires try_to_float |This| -> Float, Error!CASTABLE_TO_BOOL requires to_bool |This| -> BoolTRY_CASTABLE_TO_BOOL requires try_to_bool |This| -> Bool, Error!CASTABLE_TO_STRING requires to_string |This| -> StringTRY_CASTABLE_TO_STRING requires try_to_string |This| -> String, Error!CASTABLE_TO_CHAR requires to_char |This| -> CharTRY_CASTABLE_TO_CHAR requires try_to_char |This| -> Char, Error!CASTABLE_TO_ERROR requires to_error |This| -> ErrorTRY_CASTABLE_TO_ERROR requires try_to_error |This| -> Error, Error!
Contract
- These traits resolve without imports.
- They cannot be redeclared, imported, exported, aliased or shadowed.
- They cannot be used as values.
- The compiler registers builtin evidence for the builtin cast table.
- Same-file nominal source types and aligned generic constructors may provide evidence.
- Source method implementations use lowercase
this and the concrete receiver type. - Explicit conformance is required.
- Infallible and fallible pairs for one target are incompatible.
- Evidence resolves before HIR.
- The traits prove source evidence only.
- They do not create user-defined target types or a general conversion framework.
Example
Label = |
text String,
|
to_string |this Label| -> String:
return this.text
;
Label must CASTABLE_TO_STRING
value String = cast Label("docs")
Infallible versus fallible
Label must CASTABLE_TO_STRING
Label must TRY_CASTABLE_TO_INT
A fallible conformance requires the matching try_to_* method. The method returns success with return and failure with return!:
Label = |
text String,
|
try_to_int |this Label| -> Int, Error!:
value Int = cast! this.text
return value
;
Label must TRY_CASTABLE_TO_INT
number Int = cast Label("42") catch |err|:
then 0
;
Propagation through a compatible Error! function uses cast!:
parse_label |label Label| -> Int, Error!:
value Int = cast! label
return value
;
cast! and catch are mutually exclusive. cast! ... catch is rejected.
Related concepts