Skip to content

User Defined Casts

IsaacShelton edited this page Mar 21, 2022 · 5 revisions

User Defined Casts

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 {
    // ...
}

Implicit Conversions

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.

Manual Function Invocation

User-defined conversions can be manually invoked using the ~> operator to specify the return type:

__as__(from) ~> ToType
Clone this wiki locally