summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2018-05-12 06:11:24 +0200
committerEugen Wissner <belka@caraus.de>2018-05-12 06:11:24 +0200
commitfaf952b30ee5055af7df6ae5b4cba5b8406780ba (patch)
tree2a1dd5be4549486cbcbd598f4cb5cb4ac0a095ba
parent53620cdddfd40ad997af8c9c273ce1a68a284881 (diff)
downloadtanya-faf952b30ee5055af7df6ae5b4cba5b8406780ba.tar.gz
Rename Pair to Tuple
-rw-r--r--source/tanya/typecons.d91
1 files changed, 74 insertions, 17 deletions
diff --git a/source/tanya/typecons.d b/source/tanya/typecons.d
index d92cc4d..c828ab0 100644
--- a/source/tanya/typecons.d
+++ b/source/tanya/typecons.d
@@ -17,10 +17,67 @@
*/
module tanya.typecons;
-import tanya.meta.metafunction;
+import tanya.meta.metafunction : AliasSeq, AliasTuple = Tuple, Map;
+
+deprecated("Use tanya.typecons.Tuple instead")
+template Pair(Specs...)
+{
+ template parseSpecs(size_t fieldCount, Specs...)
+ {
+ static if (Specs.length == 0)
+ {
+ alias parseSpecs = AliasSeq!();
+ }
+ else static if (is(Specs[0]) && fieldCount < 2)
+ {
+ static if (is(typeof(Specs[1]) == string))
+ {
+ alias parseSpecs
+ = AliasSeq!(AliasTuple!(Specs[0], Specs[1]),
+ parseSpecs!(fieldCount + 1, Specs[2 .. $]));
+ }
+ else
+ {
+ alias parseSpecs
+ = AliasSeq!(AliasTuple!(Specs[0]),
+ parseSpecs!(fieldCount + 1, Specs[1 .. $]));
+ }
+ }
+ else
+ {
+ static assert(false, "Invalid argument: " ~ Specs[0].stringof);
+ }
+ }
+
+ alias ChooseType(alias T) = T.Seq[0];
+ alias ParsedSpecs = parseSpecs!(0, Specs);
+
+ static assert(ParsedSpecs.length == 2, "Invalid argument count");
+
+ struct Pair
+ {
+ /// Field types.
+ alias Types = Map!(ChooseType, ParsedSpecs);
+
+ // Create field aliases.
+ static if (ParsedSpecs[0].length == 2)
+ {
+ mixin("alias " ~ ParsedSpecs[0][1] ~ " = expand[0];");
+ }
+ static if (ParsedSpecs[1].length == 2)
+ {
+ mixin("alias " ~ ParsedSpecs[1][1] ~ " = expand[1];");
+ }
+
+ /// Represents the values of the $(D_PSYMBOL Pair) as a list of values.
+ Types expand;
+
+ alias expand this;
+ }
+}
/**
- * $(D_PSYMBOL Pair) can store two heterogeneous objects.
+ * $(D_PSYMBOL Tuple) can store two heterogeneous objects.
*
* The objects can by accessed by index as $(D_INLINECODE obj[0]) and
* $(D_INLINECODE obj[1]) or by optional names (e.g.
@@ -34,7 +91,7 @@ import tanya.meta.metafunction;
* Params:
* Specs = Field types and names.
*/
-template Pair(Specs...)
+template Tuple(Specs...)
{
template parseSpecs(size_t fieldCount, Specs...)
{
@@ -47,13 +104,13 @@ template Pair(Specs...)
static if (is(typeof(Specs[1]) == string))
{
alias parseSpecs
- = AliasSeq!(Tuple!(Specs[0], Specs[1]),
+ = AliasSeq!(AliasTuple!(Specs[0], Specs[1]),
parseSpecs!(fieldCount + 1, Specs[2 .. $]));
}
else
{
alias parseSpecs
- = AliasSeq!(Tuple!(Specs[0]),
+ = AliasSeq!(AliasTuple!(Specs[0]),
parseSpecs!(fieldCount + 1, Specs[1 .. $]));
}
}
@@ -68,7 +125,7 @@ template Pair(Specs...)
static assert(ParsedSpecs.length == 2, "Invalid argument count");
- struct Pair
+ struct Tuple
{
/// Field types.
alias Types = Map!(ChooseType, ParsedSpecs);
@@ -83,7 +140,7 @@ template Pair(Specs...)
mixin("alias " ~ ParsedSpecs[1][1] ~ " = expand[1];");
}
- /// Represents the values of the $(D_PSYMBOL Pair) as a list of values.
+ /// Represents the values of the $(D_PSYMBOL Tuple) as a list of values.
Types expand;
alias expand this;
@@ -93,7 +150,7 @@ template Pair(Specs...)
///
@nogc nothrow pure @safe unittest
{
- auto pair = Pair!(int, "first", string, "second")(1, "second");
+ auto pair = Tuple!(int, "first", string, "second")(1, "second");
assert(pair.first == 1);
assert(pair[0] == 1);
assert(pair.second == "second");
@@ -102,16 +159,16 @@ template Pair(Specs...)
@nogc nothrow pure @safe unittest
{
- static assert(is(Pair!(int, int)));
- static assert(!is(Pair!(int, 5)));
+ static assert(is(Tuple!(int, int)));
+ static assert(!is(Tuple!(int, 5)));
- static assert(is(Pair!(int, "first", int)));
- static assert(is(Pair!(int, "first", int, "second")));
- static assert(is(Pair!(int, "first", int)));
+ static assert(is(Tuple!(int, "first", int)));
+ static assert(is(Tuple!(int, "first", int, "second")));
+ static assert(is(Tuple!(int, "first", int)));
- static assert(is(Pair!(int, int, "second")));
- static assert(!is(Pair!("first", int, "second", int)));
- static assert(!is(Pair!(int, int, int)));
+ static assert(is(Tuple!(int, int, "second")));
+ static assert(!is(Tuple!("first", int, "second", int)));
+ static assert(!is(Tuple!(int, int, int)));
- static assert(!is(Pair!(int, "first")));
+ static assert(!is(Tuple!(int, "first")));
}