summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2018-09-23 06:58:26 +0200
committerEugen Wissner <belka@caraus.de>2018-09-23 06:59:41 +0200
commit9ac56c50f16cf884eefb0f3477e657b455ad14f8 (patch)
treee162727402bca672d913566b6351b22f046b6abf
parent03b45ae4412915edd9741812e477f809ba815a50 (diff)
downloadtanya-9ac56c50f16cf884eefb0f3477e657b455ad14f8.tar.gz
typecons: Add option constructor function
-rw-r--r--source/tanya/typecons.d29
1 files changed, 29 insertions, 0 deletions
diff --git a/source/tanya/typecons.d b/source/tanya/typecons.d
index db1abfa..52fcbb2 100644
--- a/source/tanya/typecons.d
+++ b/source/tanya/typecons.d
@@ -178,6 +178,8 @@ template tuple(Names...)
*
* Params:
* T = Type of the encapsulated value.
+ *
+ * See_Also: $(D_PSYMBOL option).
*/
struct Option(T)
{
@@ -530,3 +532,30 @@ struct Option(T)
assert(toHash.toHash() == 1U);
}
}
+
+/**
+ * Creates a new $(D_PSYMBOL Option).
+ *
+ * Params:
+ * T = Option type.
+ * value = Initial value.
+ *
+ * See_Also: $(D_PSYMBOL Option).
+ */
+Option!T option(T)(auto ref T value)
+{
+ return Option!T(forward!value);
+}
+
+/// ditto
+Option!T option(T)()
+{
+ return Option!T();
+}
+
+///
+@nogc nothrow pure @safe unittest
+{
+ assert(option!int().isNothing);
+ assert(option(5) == 5);
+}