-
-
Notifications
You must be signed in to change notification settings - Fork 9
User Defined Casts
Isaac Shelton edited this page Oct 24, 2020
·
5 revisions
User defined casts can be created by defining a function named __as__, with an argument of the "from" type and a return type of the "to" type:
func __as__(from FromType) ToType {
// ...
}
By default, user-defined casts only occur when as or cast is used on a value of the "from" type:
from as ToType
cast ToType from
In order for conversion between two types to be implicit and not require as or cast, you can use the implicit keyword when defining the conversion:
implicit func __as__(from FromType) ToType {
// ...
}
This will allow for values of type FromType to be automatically converted to the type ToType.
The __as__ functions can also be called directly if so desired:
__as__(from) ~> ToType