spool-lang / Spool-Legacy-Repo

Legacy repo for the Spool programming language.
3 stars 0 forks source link

Optional #20

Open RedstoneParadox opened 5 years ago

RedstoneParadox commented 5 years ago

Overview

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'
}