dmd.root.optional

Implementation of an 'Optional' type

Authors

Walter Bright

Source: root/optional.d

Examples

  1. import core.exception : AssertError; Optional!int opt; assert( opt.isEmpty()); assert(!opt.isPresent()); assert(!opt.hasValue(1)); assert(!opt.hasValue(2)); bool caught; try cast(void) opt.get(); catch (AssertError) caught = true; assert(caught); opt = Optional!int(1); assert(!opt.isEmpty()); assert( opt.isPresent()); assert( opt.get() == 1); assert( opt.hasValue(1)); assert(!opt.hasValue(2));

  • Declaration

    struct Optional(T);

    Optional type that is either empty or contains a value of type T

    • Declaration

      this(T value);
      static Optional!T create(T val);

      Creates an Optional with the given value

    • Declaration

      const bool isPresent();

      Return Value

      Whether this Optional contains a value

    • Declaration

      const bool isEmpty();

      Return Value

      Whether this Optional does not contain a value

    • get

      Declaration

      inout inout(T) get();

      Return Value

      The value if present

    • Declaration

      const bool hasValue(const T exp);

      Return Value

      Whether this Optional contains the supplied value