How to create TypeTuple/ExpressionTuple from tuple/tuples

Philippe Sigaud philippe.sigaud at gmail.com
Wed Aug 8 22:02:05 PDT 2012


On Thu, Aug 9, 2012 at 1:26 AM, Kenji Hara <k.hara.pg at gmail.com> wrote:

> You can also use slice operator instead of expand property.

>
> import std.stdio, std.typecons;
>
> void f(T ...)(T t) {
>     writeln(t.length);
> }
>
> void main(){
>
>     auto v = tuple(1, 2, 3);
>     f(v[]); // prints 3

I didn't know that.
Nice one. Indeed, it's almost like a range. A bit dangerous, as people
might forget that what they get is a tuple with possibly different
types, instead of a range, but good to know as it offers a homogeneous
syntax.

Hey, I have map/filter/reduce functions for tuples, should they be
part of Phobos?

auto t = tupleMap!(a => to!string(a))(1,'a',"abc'", 3.14); // eager, not lazy

assert(t == tuple("1", "a", "abc", "3.14"));

auto arr = [t[]]; // crack open & transform into an array
assert(is(typeof(arr) == string[]));


Of course, the mapping function must be polymorphic or overloaded. The
good news is, it may very well have different return types for each
argument type, thus giving back another, entirely different tuple.

But I'm not sure people use tuples that much in D...
>     auto v0 = tuple(1, 2, 3);
>     auto v1 = tuple(4, 5, 6);
>     f(v0[], v1[]);  // prints 6
> }
>


More information about the Digitalmars-d-learn mailing list