# dmd.root.optional

Implementation of an 'Optional' type

#### Authors

Walter Bright

#### License

Boost License 1.0

Source: root/optional.d

Documentation: https://dlang.org/phobos/dmd_root_optional.html

Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/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));`

- `Optional`

  #### Declaration

  `struct `Optional`(T);`

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

  - `this`

    #### Declaration

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

    Creates an `Optional` with the given `value`

  - `isPresent`

    #### Declaration

    `const bool `isPresent`();`

    #### Return Value

    Whether this `Optional` contains a value

  - `isEmpty`

    #### 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

  - `hasValue`

    #### Declaration

    `const bool `hasValue`(const T `exp`);`

    #### Return Value

    Whether this `Optional` contains the supplied value