Struct Optional

Optional type. Also known as a Maybe or Option type in some languages.

struct Optional(T) ;

This can either contain a value or be none. If the value is a refernce type then null is considered none.

It also has range like behavior. So this acts as a range that contains 1 element or is empty.

And all operations that can be performed on a T can also be performed on an Optional!T. The behavior of applying an operation on a no-value or null pointer is well defined and safe.

Constructors

NameDescription
this (value) Constructs an Optional!T value by assigning T

Methods

NameDescription
opAssign () Assigns a value to the optional or sets it to none.
opBinary (rhs) If the optional is some value it returns an optional of some value op rhs
opBinaryRight (lhs) If the optional is some value it returns an optional of some lhs op value
opCall (args) If there's a value that's callable it will be called else it's a noop
opDollar () Provides indexing into arrays
opEquals () Compare two optionals or an optional with some value
opIndex (index) Provides indexing into arrays
opOpAssign (rhs) If the optional is some value, op assigns rhs to it
opSlice (begin, end) Provides indexing into arrays
opUnary () Applies unary operator to internal value of optional.
toString () Converts value to string