Optional<T> is a sealed class () used to represent situations where an instance/value of T may not be present. It has two sub-classes: None<T>, which is an object class (#?) used to represent no value, and Some<T>, which is a class used to represent some T value.
For ergonomics, Silicon has T? as shorthand for Optional<T> and the keyword none for None<T>. Additionally, Some<T> implements Box<T> (#19 ), so it can be treated as an instance of T due to implicit boxing and unboxing.
main {
const noneFoo : Foo? = none
const someFoo : Foo? = new Foo()
print(noneFoo is none) // Prints 'true'
print(noneFoo is Foo) // Prints 'false'
print(someFoo is Foo) // Prints 'true'
}
Overview
Optional<T>
is a sealed class () used to represent situations where an instance/value ofT
may not be present. It has two sub-classes:None<T>
, which is an object class (#?) used to represent no value, andSome<T>
, which is a class used to represent someT
value.For ergonomics, Silicon has
T?
as shorthand forOptional<T>
and the keywordnone
forNone<T>
. Additionally,Some<T>
implementsBox<T>
(#19 ), so it can be treated as an instance ofT
due to implicit boxing and unboxing.