Yet another MRV proposal!

Bill Baxter dnewsgroup at billbaxter.com
Tue Apr 15 06:21:14 PDT 2008


Janice Caron wrote:
> Later, Andrei came along and wrote std.typecons, which uses the word
> Tuple correctly (i.e. the same way it's used in every other
> programming language under the sun).
>...  The first ten fields
> may be accessed with ._0 to ._9. Any field may be accessed as
> .field[n]. Any /named/ field may be accessed by name.

I don't recall tuples working like that in any language I've used. 
Named tuple fields?  No [#] access?  Sounds more like a struct.

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";
     writefln("x = ", x);
     writefln("type = ", typeof(x).stringof);


 >> x = 32.5hello
 >> type = (int, float, invariant(char)[])


Ok, the way it prints is odd, but that's pretty much what most languages 
call a tuple.  That's what they're like in ML and Python at least, which 
is where I've seen 'em.  They aren't structs pretending to be tuples. 
They're actual tuples.  An ordered, fixed-sized list of anonymous items 
of fixed, but arbitrary type.

As for the weird printing, that's really the fault of automatic 
flattening of tuples.  That was probably a bad idea.  Users should have 
to do something to explode a tuple to make it act like a list of arguments.

The other thing TypeTuple lacks is a literal value syntax.  You can only 
  initialize them one-by-one like that.  Or by creating a struct and 
using .tupleof on it.  Which means you can use the /other/ Tuple for 
that if you define the type first:

     struct S {int a;float b;string c;}
     S s = S(101,3.14,"bye");
     x = s.tupleof;

or
     alias Tuple!(int,float,string) T; // [1]
     T t = T(451,2.718,"struct");
     x = t.tupleof;

My opinion is that it's Andrei who's abusing the terminology by calling 
a struct that pretends to be a tuple a Tuple.


[1] actually that doesn't work.  Known bug?  The example in the 
documentation:
     alias Tuple!(string, string) DicEntry; // names can be omitted
doesn't compile either.  (Dmd 2.012)

--bb



More information about the Digitalmars-d mailing list