You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Isaac Shelton edited this page Sep 8, 2023
·
2 revisions
Loose Polymorph
Loose polymorphs are a type of polymorph that are prefixed with $~ instead of $.
$~T
$~IndexType
Loose polymorphs will match to the first compatible type, but are also flexible to allow for potential conversion between primitive types. Loose polymorphs will match if the original $T and new $T are both integers or both floats.
record <$T> Single (value $T)
func sum(a, b <$~T> Single) <$T> Single {
return Single(a.value + b.value)
}
For example, the above function can be called as sum(Single(10uz), Single(13si)) or sum(Single('A'ub), Single(3)). The first type match will be $T/$~T, so sum(Single(10uz), Single(13si)) would return a <usize> Single and sum(Single('A'ub), Single(3)) would return a <ubyte> Single.
Loose polymorph matching via $~T is not allowed between integers and floats. So calling it as sum(Single('A'ub), Single(3.0f)) would not resolve to the above function.