Tuple basics

Sergey Gromov snake.scaly at gmail.com
Sat Sep 13 02:48:03 PDT 2008


Sam Hu <samhu.samhu at gmail.com> wrote:
> I seached questions regarding Tuple in this newsgroup but did not find 
> the the same question I have.Since I am a newbie ,they are too advanced 
> to me at this moment.My question is just :What is tuple?what can tuple 
> do in D while  other languae like c++ can not do or can not do well?
> Could somebody please post a very simple code segment and an simple but 
> enough explanation so that I can understand what the hell a tuple is .

Many already tried to explain, let me try, too.  :)

I'll put it this way.

When you write a regular program, you pass values to functions.  
Sometimes you want to pass many values as a single entity. Then you use 
lists/arrays/structs/classes.

When you write a template, you pass types to other templates.  Sometimes 
you want to pass many types as a single entity.  Then you use tuples.  
Because templates can have value arguments, tuples can contain compile-
time values, too.

Usually you create tuples with a special template syntax, when you say 
you want to get a tuple instead of individual template agruments.  Very 
much like in vararg function you receive all arguments in a sort of list 
instead of as individual entities.  There are also language constructs 
or library functions which allow you to create tuples of struct types, 
struct values, function argument types etc.  They're all intended for 
passing as template arguments at compile time.

There's a special case, or trick, that if you specify a tuple which 
contains only compile-time values as an argument to a regular function, 
this is the same as if you specified all those values as separate 
arguments to that function.

All this is quite hard to get a grip on if you don't actually need and 
write complex template code, so you can safely ignore all that tuple 
stuff.


More information about the Digitalmars-d-learn mailing list