Skip to content

Loose Polymorph

IsaacShelton edited this page Mar 21, 2022 · 2 revisions

Loose Polymorph

Loose polymorphs are a type of polymorph that are prefixed with $~ instead of $.

$~T
$~IndexType

They will match to the first compatible type, but they also allow for some conversion between primitive types. Conversion of primitive types is allowed if the original $T and new $T are both integers or both floats.

func sum(a, b $~T) $T {
    return a + b
}

For example, the above function can be called as sum(10uz, 13si) or sum('A'ub, 3). The return type is the first match of $T/$~T, so the sum(10uz, 13si) would return a usize and sum('A'ub, 3) would return a ubyte.

Implicit primitive conversion via $~T is not allowed between integers and floats. So calling it as sum('A'ub, 3.0f) would not resolve to the above function.

Clone this wiki locally