www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - tuple trouble

reply =?iso-8859-1?q?Knud_S=F8rensen?= <12tkvvb02 sneakemail.com> writes:
I am having some trouble to see if tuples types would be a good idea in d.

So, I would like to hear your opinions. 
  

First we already uses tuples for function arguments like:
 
  func_1(elem_1, elem_2,elem_3);

But what if one could do something like.

  tuple arg= new tuple(elem_1,elem_2,elem_3);


  res1=func_1 arg;
  ...
  res_n=func_n arg;

Would that be useful at all ??

Then if functions could return tuples we could write:

  res_21=func_2 func_1 arg; // for the combined function
	

It would be more like the mathematical notation func_2 o func_1(arg)
but would it just lead to more spaghetti code ?
What do you think ?
Oct 30 2005
parent reply Georg Wrede <georg.wrede nospam.org> writes:
Knud Sørensen wrote:
 I am having some trouble to see if tuples types would be a good idea
 in d. So, I would like to hear your opinions.
 
 First we already uses tuples for function arguments like:
 
 func_1(elem_1, elem_2,elem_3);
 
 But what if one could do something like.
 
 tuple arg= new tuple(elem_1,elem_2,elem_3);
 
 res1=func_1 arg; ... res_n=func_n arg;
 
 Would that be useful at all ??
 
 Then if functions could return tuples we could write:
 
 res_21=func_2 func_1 arg; // for the combined function  It would be
 more like the mathematical notation func_2 o func_1(arg) but would it
 just lead to more spaghetti code ? What do you think ?

This Tuple Thing pops up at regular intervals. Of course it would be nice to have fluid tuples (a la Euphoria, for example). The you could write: real [] foo = sin(2.3, 0.2, 0.002, 0.55); But then, can any of the proponents imagine what kind of rewriting of the entire language (and rewriting of the entire front-end) this would require? Plus, implementing this would strain (as in making slower) everything we do with the language. Out of which 99% would still be "conventional", that is, functions returning exactly one value anyhow. If somebody really wanted to do this Tuple Thing, then it would not be too hard to write a library for Tuple Proof Functions. Those would take 1..n arguments and return (possibly, depending on the context) 1..n results. No prob, D is well suited to writing that library. Then we could have something like: typedef Object [] tuple; tuple sin(...) // whatever, probably a 0..n list of reals { tuple daStuff = new tuple(); // so munch a list of arguments, one at a time // if it's a real, do sin it // else do something proper or polite with it // finally return the generated pile of stool return daStuff; } Thus, the question arises, do we really have to interfere with the language itself to get this done, or can we just write the library?
Oct 30 2005
parent reply =?ISO-8859-1?Q?Jari-Matti_M=E4kel=E4?= <jmjmak invalid_utu.fi> writes:
Georg Wrede wrote:
 Thus, the question arises, do we really have to interfere with the 
 language itself to get this done, or can we just write the library?

I don't think there's much point in making the language any slower. It's pretty convenient to implement this as a separate library. If Walter is able to merge this syntax into the language without much trouble / performance issues, it might be even useful. BTW, does Walter have any plans to support multiple function return values in the future?
Oct 31 2005
parent reply Georg Wrede <georg.wrede nospam.org> writes:
Jari-Matti Mäkelä wrote:
 Georg Wrede wrote:
 
 Thus, the question arises, do we really have to interfere with the
 language itself to get this done, or can we just write the
 library?

I don't think there's much point in making the language any slower. It's pretty convenient to implement this as a separate library. If Walter is able to merge this syntax into the language without much trouble / performance issues, it might be even useful.

Exactly. So, those who want the library can write it right now, and this would let Walter do more important stuff. Besides, living with multiple return values is a "way of life", which can already be done in D. It's a different way of thinking, just like OO, structural, procedural, generic, and whatever-programming.
Oct 31 2005
parent Sean Kelly <sean f4.ca> writes:
Georg Wrede wrote:
 
 Besides, living with multiple return values is a "way of life", which 
 can already be done in D. It's a different way of thinking, just like 
 OO, structural, procedural, generic, and whatever-programming.

"auto" makes this very easy to do too: Tuple!(int,int,int) getStuff(); auto ret = getStuff(); int a = ret.val!(1); int b = ret.val!(2); int c = ret.val!(3); Sean
Oct 31 2005