Dart DocumentationpersistentOption<T>

Option<T> class

class Option<T> {
 final T _value;
 final bool isDefined;

 Option._internal(this.isDefined, this._value);

 factory Option.none() => new Option._internal(false, null);

 factory Option.some(T value) => new Option._internal(true, value);

 T get value {
   if (isDefined) return _value;
   throw "undefined";
 }

 // forall U, Option<U> map(U f(T))
 Option map(f(T)) =>
     isDefined ? new Option.some(f(this.value)) : this;

 bool operator ==(Option<T> other) =>
     (isDefined && other.isDefined && _value == other._value)
  || (!isDefined && !other.isDefined);

 String toString() =>
     isDefined ? "Option.some($_value)" : "Option.none()";
}

Constructors

factory Option.none() #

factory Option.none() => new Option._internal(false, null);

factory Option.some(T value) #

factory Option.some(T value) => new Option._internal(true, value);

Properties

final bool isDefined #

final bool isDefined;

final T value #

T get value {
 if (isDefined) return _value;
 throw "undefined";
}

Operators

bool operator ==(Option<T> other) #

The equality operator.

The default behavior for all Objects is to return true if and only if this and other are the same object.

If a subclass overrides the equality operator it should override the hashCode method as well to maintain consistency.

docs inherited from Object
bool operator ==(Option<T> other) =>
   (isDefined && other.isDefined && _value == other._value)
|| (!isDefined && !other.isDefined);

Methods

Option map(f(T)) #

Option map(f(T)) =>
   isDefined ? new Option.some(f(this.value)) : this;

String toString() #

Returns a string representation of this object.

docs inherited from Object
String toString() =>
   isDefined ? "Option.some($_value)" : "Option.none()";