Add typecons.tuple(), Tuple construction function

This commit is contained in:
Eugen Wissner 2018-09-22 07:32:30 +02:00
parent 31d4f30a49
commit 03b45ae441

View File

@ -19,6 +19,7 @@ module tanya.typecons;
import tanya.algorithm.mutation; import tanya.algorithm.mutation;
import tanya.format; import tanya.format;
import tanya.functional;
import tanya.meta.metafunction; import tanya.meta.metafunction;
import tanya.meta.trait; import tanya.meta.trait;
@ -35,6 +36,8 @@ import tanya.meta.trait;
* *
* Params: * Params:
* Specs = Field types and names. * Specs = Field types and names.
*
* See_Also: $(D_PSYMBOL tuple).
*/ */
template Tuple(Specs...) template Tuple(Specs...)
{ {
@ -133,6 +136,43 @@ template Tuple(Specs...)
static assert(!is(Tuple!(int, "first", double, "second", char, "third"))); static assert(!is(Tuple!(int, "first", double, "second", char, "third")));
} }
/**
* Creates a new $(D_PSYMBOL Tuple).
*
* Params:
* Names = Field names.
*
* See_Also: $(D_PSYMBOL Tuple).
*/
template tuple(Names...)
{
/**
* Creates a new $(D_PSYMBOL Tuple).
*
* Params:
* Args = Field types.
* args = Field values.
*
* Returns: Newly created $(D_PSYMBOL Tuple).
*/
auto tuple(Args...)(auto ref Args args)
if (Args.length >= Names.length && isTypeTuple!Args)
{
alias Zipped = ZipWith!(AliasSeq, Pack!Args, Pack!Names);
alias Nameless = Args[Names.length .. $];
return Tuple!(Zipped, Nameless)(forward!args);
}
}
///
@nogc nothrow pure @safe unittest
{
auto t = tuple!("one", "two")(20, 5);
assert(t.one == 20);
assert(t.two == 5);
}
/** /**
* $(D_PSYMBOL Option) is a type that contains an optional value. * $(D_PSYMBOL Option) is a type that contains an optional value.
* *