diff options
| author | Eugen Wissner <belka@caraus.de> | 2018-03-21 08:14:52 +0100 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2018-03-21 08:14:52 +0100 |
| commit | 1f4ab88254635328910ec4494166dc4d2d6bbee9 (patch) | |
| tree | 618b45f9443ae578c92a5564c05afe26c2a4a1c9 | |
| parent | 7af5b4db7266dea76fd883dd24224d2f1e7426b5 (diff) | |
| download | tanya-1f4ab88254635328910ec4494166dc4d2d6bbee9.tar.gz | |
typecons.Pair: Add better documentation unittests
| -rw-r--r-- | source/tanya/typecons.d | 38 |
1 files changed, 23 insertions, 15 deletions
diff --git a/source/tanya/typecons.d b/source/tanya/typecons.d index 68cc589..d92cc4d 100644 --- a/source/tanya/typecons.d +++ b/source/tanya/typecons.d @@ -8,7 +8,7 @@ * This module contains templates that allow to build new types from the * available ones. * - * Copyright: Eugene Wissner 2017. + * Copyright: Eugene Wissner 2017-2018. * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/, * Mozilla Public License, v. 2.0). * Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner) @@ -36,7 +36,7 @@ import tanya.meta.metafunction; */ template Pair(Specs...) { - template parseSpecs(int fieldCount, Specs...) + template parseSpecs(size_t fieldCount, Specs...) { static if (Specs.length == 0) { @@ -47,13 +47,13 @@ template Pair(Specs...) static if (is(typeof(Specs[1]) == string)) { alias parseSpecs - = AliasSeq!(Specs[0], + = AliasSeq!(Tuple!(Specs[0], Specs[1]), parseSpecs!(fieldCount + 1, Specs[2 .. $])); } else { alias parseSpecs - = AliasSeq!(Specs[0], + = AliasSeq!(Tuple!(Specs[0]), parseSpecs!(fieldCount + 1, Specs[1 .. $])); } } @@ -63,25 +63,24 @@ template Pair(Specs...) } } + 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 = parseSpecs!(0, Specs); - - static assert(Types.length == 2, "Invalid argument count."); + alias Types = Map!(ChooseType, ParsedSpecs); // Create field aliases. - static if (is(typeof(Specs[1]) == string)) - { - mixin("alias " ~ Specs[1] ~ " = expand[0];"); - } - static if (is(typeof(Specs[2]) == string)) + static if (ParsedSpecs[0].length == 2) { - mixin("alias " ~ Specs[2] ~ " = expand[1];"); + mixin("alias " ~ ParsedSpecs[0][1] ~ " = expand[0];"); } - else static if (is(typeof(Specs[3]) == string)) + static if (ParsedSpecs[1].length == 2) { - mixin("alias " ~ Specs[3] ~ " = expand[1];"); + mixin("alias " ~ ParsedSpecs[1][1] ~ " = expand[1];"); } /// Represents the values of the $(D_PSYMBOL Pair) as a list of values. @@ -94,6 +93,15 @@ template Pair(Specs...) /// @nogc nothrow pure @safe unittest { + auto pair = Pair!(int, "first", string, "second")(1, "second"); + assert(pair.first == 1); + assert(pair[0] == 1); + assert(pair.second == "second"); + assert(pair[1] == "second"); +} + +@nogc nothrow pure @safe unittest +{ static assert(is(Pair!(int, int))); static assert(!is(Pair!(int, 5))); |
