Variant<T, U>, Variant<T, U, V>, and so on are special sealed classes (#?) used to represent situations where a value may be one of multiple types. It's sub-classes VariantT, VariantU, etc. are used to represent a value of each of these types.
For ergonomics, Silicon has T | U as shorthand for Variant<T, U> and so on. All subclasses of Variant also implement Box<T>(#19), so they get the benefits of implicit boxing and unboxing.
main {
const variantFoo : Foo | Bar = new Foo();
const variantBar : Foo | Bar = new Bar();
println(variantFoo is Foo) // Prints 'true'
println(variantBar is Foo) // Prints 'false'
}
Overview
Variant<T, U>
,Variant<T, U, V>
, and so on are special sealed classes (#?) used to represent situations where a value may be one of multiple types. It's sub-classesVariantT
,VariantU
, etc. are used to represent a value of each of these types.For ergonomics, Silicon has
T | U
as shorthand forVariant<T, U>
and so on. All subclasses ofVariant
also implementBox<T>
(#19), so they get the benefits of implicit boxing and unboxing.