2 * Optional implementation.
4 * Copyright: Copyright (C) 1999-2021 by The D Language Foundation, All Rights Reserved
5 * Authors: $(LINK2 http://www.digitalmars.com, Walter Bright)
6 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
7 * Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/root/optional.d, root/_optional.d)
8 * Documentation: https://dlang.org/phobos/dmd_root_optional.html
9 * Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/root/optional.d
11 module dmd
.root
.optional
;
16 import core
.exception
: AssertError
;
19 assert( opt
.isEmpty());
20 assert(!opt
.isPresent());
21 assert(!opt
.hasValue(1));
22 assert(!opt
.hasValue(2));
31 opt
= Optional
!int(1);
32 assert(!opt
.isEmpty());
33 assert( opt
.isPresent());
34 assert( opt
.get() == 1);
35 assert( opt
.hasValue(1));
36 assert(!opt
.hasValue(2));
39 /// Optional type that is either `empty` or contains a value of type `T`
40 extern (C
++) struct Optional(T
)
42 /// the value (if present)
45 /// whether `value` is set
48 /// Creates an `Optional` with the given value
55 // Ctor wrapper for the C++ interface (required by older host compilers)
57 static Optional
!T
create(T val
)
59 return Optional
!T(val
);
62 /// Returns: Whether this `Optional` contains a value
63 bool isPresent() const
68 /// Returns: Whether this `Optional` does not contain a value
74 /// Returns: The value if present
81 /// Returns: Whether this `Optional` contains the supplied value
82 bool hasValue(const T exp
) const
84 return present
&& value
== exp
;