Yet another MRV proposal!
Bill Baxter
dnewsgroup at billbaxter.com
Tue Apr 15 07:14:22 PDT 2008
Janice Caron wrote:
> On 15/04/2008, Bill Baxter <dnewsgroup at billbaxter.com> wrote:
>> TypeTuples are much closer to what is called a tuple in other languages.
>>
>> TypeTuple!(int,float,string) x;
>> x[0] = 3;
>> x[1] = 2.5;
>> x[2] = "hello";
>
> Where is TypeTuple! defined?
std.typetuple
It's just the classic "Walter tuple" as you put it though. Just called
TypeTuple.
>> An ordered, fixed-sized list of anonymous items of fixed,
>> but arbitrary type.
>
> Yep, that's a std.typecons.Tuple.
Except the anonymous part. And the list part. Lists use [i] for
indexing, not something weird like .field!(i). ((But this is D so I
should have said "array" rather than "list", I guess))
> A "Walter Tuple" is a list of arbitrary values and types, for example
> ( int, float, 42, "hello" ).
But that won't compile in places where a type is needed, or where a
value is needed. Just like "int x = float;" won't compile. Doesn't
seem a problem.
> An "Andrei Tuple" is an ordered, fixed-sized list of items of fixed
> but arbitrary type.
An "Andrei Tuple" is a struct. So if you want to define structs to be
tuples, then I guess that's your prerogative. I don't mean they are
like structs, I mean they *are* structs. The definition is
struct Tuple(T...) {
// bunch of string mixins to automatically
// generate member names from types in T
}
It's nifty as a workaround for lack of real, usable tuples, but that's
all it is. So there's really no such thing as an anonymous element of
an Andrei Tuple. It's a struct, so they all have names, just not names
you gave them.
Some other things that I would not expect of a real tuple:
* is(AndreTuple == struct) returns true.
* cannot slice it (except maybe with the likes of AndreiTuple.tupleof[1..$])
* __traits(allMembers,AndreiTuple) introspection reveals a lot of cruft
like [_0 field _1 toString __T9tupleImplVi0TiTfZ]
One advantage AndreiTuples do have is that they can be nested without
the dreaded automatic flattening. At least I assume they can, since
they're just structs. But nesting is good.
So really I suspect what we'd all like is a combination of aspects of
"Walter Tuples" and "Andrei Tuples".
--bb
More information about the Digitalmars-d
mailing list