Speaking of which, I think there's a typo in Phobos docs: http://www.digitalmars.com/d/2.0/phobos/std_typecons.html
Tuple!(int, int) point; // assign coordinates point.field[0] = 5; point.field[1] = 6; // read coordinates auto x = point.field[0]; auto y = point.[1]; The last one should be "auto y = point.field[1]" On Fri, Jul 30, 2010 at 8:53 PM, Andrej Mitrovic <[email protected] > wrote: > > I've found that the syntax mytuple.field[] can be used, I think this is > probably what you meant by .first and .second. > > > On Fri, Jul 30, 2010 at 8:20 PM, Andrej Mitrovic < > [email protected]> wrote: > >> Hey, just tried out your code. >> >> In the first one there's a little typo (used 'i' instead of index), but >> that's ok. ;p >> >> It seems like I don't need to use the to!() template. This code seems to >> work fine: >> >> >> void gun(T)(T value) { >> foreach(Type; value.expand) >> write(Type, " "); >> >> writeln(); >> } >> >> But regardless, at least I now know how to traverse through my own tuples, >> so thanks for that one. >> >> In your second example: >> >> >> Tuple!("index", int, "sum", double) t = tuple(1,3.14); >> >> assert(t.index== 1); // t has .first and .second as members. >> assert(t.sum== 3.14); >> >> I get an error way deep in the library itself (this could call for a >> better error message, methinks): >> C:\DMD\dmd2\windows\bin\..\..\src\phobos\std\typecons.d(320): Error: tuple >> index 2 exceeds 2 >> >> I've reversed the order, put types before identifiers and this works fine >> then: >> >> Tuple!(int, "index", double, "sum") t = tuple(1, 3.14); >> >> I'm not sure what you mean by "t has .first and .second as members", I >> can't seem to access those. Maybe you meant t.index and t.sum as members? >> >> >> On Fri, Jul 30, 2010 at 12:11 PM, Philippe Sigaud < >> [email protected]> wrote: >> >>> On Thu, Jul 29, 2010 at 23:28, Andrej Mitrovic < >>> [email protected]> wrote: >>> >>>> How do I print out an expanded tuple but with spaces between the values? >>>> There's this example on page 164: >>>> >>>> import std.typecons, std.stdio; >>>> >>>> void fun(T...)(T args) { >>>> // create a tuple to pack all arguments together >>>> gun(tuple(args)); >>>> } >>>> >>>> void gun(T)(T value) { >>>> // expand the tuple back >>>> writeln(value.expand); >>>> } >>>> >>>> void main() { >>>> fun(1); >>>> fun(1, 2.2); >>>> } >>>> >>>> This prints out: >>>> 1 >>>> 12.2 >>>> >>>> But that's confusing, since the values are "glued" together when printed >>>> out. I really want to print out: >>>> 1 >>>> 1 2.2 >>>> >>>> Any ideas? >>>> >>> >>> You can iterate on the values and create the corresponding string. Note >>> that you must have a polymorphic function to map on a tuple's elements. >>> >>> void gun(T)(T value) >>> { >>> string result; >>> foreach(index, Type; value.expand) >>> { >>> result ~= to!string(value.expand[i]) ~ " "; >>> } >>> writeln(result); >>> } >>> >>> There will be a surnumerary " " at the very end, but I prefer to show the >>> way to iterate on a tuple. value is a std.typecons.Tuple, but value.expand >>> gives access to the raw (T...) tuple inside it. >>> >>> >>> >>>> >>>> One other thing. I can use the .length property for value and parameter >>>> tuples, but only if I haven't built them myself with the call to >>>> std.typecons.tuple(). For example I can call writeln(T.length) in the fun() >>>> function (where the compiler automatically constructs a tuple), but not in >>>> the gun() function. So there seems to be compiler tuples and >>>> user-constructed tuples, which are not the same. >>>> >>> >>> Yes, std.typecons.Tuple should have a length, it's trivial to add. It's >>> issue #4381 >>> >>> http://d.puremagic.com/issues/show_bug.cgi?id=4381 >>> >>> And yes, there are 'raw', compiler-managed tuples which are lists of >>> types and values 'glued' together. They are powerful, iterable, indexable, >>> slicable, know their length, etc. They may even be assignable. Ah, in fact, >>> they can hold anything that can be passed as a template argument, I guess. >>> But, being a bunch of many different types, they cannot be returned by a >>> function. That's C inheritance for you. They have no .init value neither, >>> which I found too bad for generic code. >>> >>> http://d.puremagic.com/issues/show_bug.cgi?id=4536 >>> >>> So std.typecons.Tuple provides a way to wrap values inside a struct, >>> while giving access to the types and values, and that can be returned by a >>> function. It can also have named members, which can be quite handy when you >>> return many things from a function, grouped inside a tuple. >>> >>> Tuple!("index", int, "sum", double) t = tuple(1,3.14); >>> >>> assert(t.index== 1); // t has .first and .second as members. >>> assert(t.sum== 3.14); >>> >>> >>> >>>> >>>> It's a bit of a shame that there isn't a chapter on tuples except this >>>> brief mention (I don't see it listed in the contents page). I guess I'll >>>> take a look at the implementation. >>>> >>>> >>> Andrei didn't want to talk too much about Phobos, as it was (ans still >>> is!) in flux while he was writing this. D the language will not change much >>> for some time, while the standard library is being actively transformed, if >>> only to take into account the new features that were added for the past 6 >>> months. >>> >>> btw, I like you posts, but do not always have the time to answer them. If >>> among the numerous issues you posted there there is still one that bother >>> you, do not hesitate to ask again for an answer. >>> >>> Philippe >>> >> >> >
