www.digitalmars.com         C & C++   DMDScript  

D - Tuples in D?

reply "Vathix" <vathix dprogramming.com> writes:
I haven't heard much about tuples before but when I saw them earlier in the
newsgroup, I was interested. I started writing down some notes on how it'd
work in D and I guess the best way to share it is to show my notes. Here
they are:


the function call:
foo(b, c, d);
could be written like accessing a property, with tuple's comma operator.
foo = b, c, d;

the old C comma operator behavior is still "kind of" supported:
for(b = 1, c = 2; bla; bla) {}
first the b and c are assigned,
then it creates a tuple with those values and discards it since it's not
used.
the actual tuple creation doesn't need to occur here, as compiler
optimization.

it's not necessary to use parentheses for a tuple,
but some cases it is, for precedence, like in function headers.

(int result1, char result2)somefunc(int param1)
{
 result1 = param1;
 result2 = 'c';
}

or it could be written like this:

(int, char)somefunc(int param1)
{
 return param1, 'c';
}

of course the old C comma operator couldn't be simulated in assignments
or returns, etc; but it's not really needed there, right?

it has a kind of messy syntax though, and some might find it ugly.
such as: if the return value doesn't need parentheses:
int main() {}
why should the parameter list?
int main {}
or
int main void {}

main {}
for void return, no parameters?...

I guess an empty tuple is expressed void and () ?
()main() {}
haha. instead of
void main() {}
or
void main void {}

but multiple return values and parameters must be in parentheses? or not
really?
this looks crazy.

some people wanted a "result" keyword for the return value,
it could be done by the programmer:
(int result)main() { result = 0; }

tuple with named parameters (? members?) can be accessed with the dot
operator:
(int foo = 3, int bar).foo
and even
with(int foo = 3, int bar) { bar = foo + 2; }

this might be too much of a change to add to D without a huge remake.
Sep 29 2003
parent reply lio mondobizzarro.com writes:
I for one never liked it when an apparent assignment is actually doing more than
a simple memory copy. It think it hides the impact of the function call. But
that's just me :-)

What I do like is python's way of getting multiple out values of a funtion,
kind-of similar syntax as yours:

a,b = foo(x);

would imply a function call like "foo(x,&a,&b);"

Also, this way there's no memory copy / copy-ctor call of the temporary returned
object to the variables a, b.

L.
Sep 29 2003
parent reply Sjoerd van Leent <Sjoerd_member pathlink.com> writes:
Tuples, I remember clean again. A tuple is not something more than a plain
structure of some data, so why not use a struct for it?

Besides that, a function calculates only one value as return value, being it a
struct, object or primitive, so if you want to use more out values you should
use a function like the following:

void proc ( in int x, out int y, out int z ) { ... }

Greetings,
Sjoerd van Leent

In article <bl966k$2gdc$1 digitaldaemon.com>, lio mondobizzarro.com says...
I for one never liked it when an apparent assignment is actually doing more than
a simple memory copy. It think it hides the impact of the function call. But
that's just me :-)

What I do like is python's way of getting multiple out values of a funtion,
kind-of similar syntax as yours:

a,b = foo(x);

would imply a function call like "foo(x,&a,&b);"

Also, this way there's no memory copy / copy-ctor call of the temporary returned
object to the variables a, b.

L.
Sep 29 2003
next sibling parent reply lio mondobizzarro.com writes:
Hi..

Tuples, I remember clean again. A tuple is not something more than a plain
structure of some data, so why not use a struct for it?
You don't want to create a struct for each combination of out values of function.
Besides that, a function calculates only one value as return value, being it a
struct, object or primitive, 
Is this some metaphysical rule or what? Maybe the mathematical definition of function has only 1 value, but there' nothing strange about a function that has more than 1 out value.
so if you want to use more out values you should
use a function like the following:

void proc ( in int x, out int y, out int z ) { ... }
Yes, this is what the declaration should look like, and this should stay, I guess.. But what 'bout being able to call this function the python way 'y,z = proc(x);' ?? I can't possibly be the only one that likes this :-S Cheers, L.
Sep 30 2003
parent reply Sjoerd van Leent <Sjoerd_member pathlink.com> writes:
Well OK, creating a struct for each type of return seems indeed a little bit
overdone. Maybe it's possible to create a language feature like an nonamed
struct something like the following:

{int; double} foo (int bar)
{
..

return {bar, (double)bar};
}

you should then sieve it with:

..
int x;
double y;
{x, y} foo (20);
..

Something like that?

Greetings,
Sjoerd van Leent

In article <blbbn7$2k8m$1 digitaldaemon.com>, lio mondobizzarro.com says...
Hi..

Tuples, I remember clean again. A tuple is not something more than a plain
structure of some data, so why not use a struct for it?
You don't want to create a struct for each combination of out values of function.
Besides that, a function calculates only one value as return value, being it a
struct, object or primitive, 
Is this some metaphysical rule or what? Maybe the mathematical definition of function has only 1 value, but there' nothing strange about a function that has more than 1 out value.
so if you want to use more out values you should
use a function like the following:

void proc ( in int x, out int y, out int z ) { ... }
Yes, this is what the declaration should look like, and this should stay, I guess.. But what 'bout being able to call this function the python way 'y,z = proc(x);' ?? I can't possibly be the only one that likes this :-S Cheers, L.
Sep 30 2003
parent "Nicolas Repiquet" <deadcow-remove-this free.fr> writes:
--
"Sjoerd van Leent" <Sjoerd_member pathlink.com> a écrit dans le message
news: blbi93$2t5g$1 digitaldaemon.com...
 Well OK, creating a struct for each type of return seems indeed a little
bit
 overdone. Maybe it's possible to create a language feature like an nonamed
 struct something like the following:

 {int; double} foo (int bar)
 {
 ..

 return {bar, (double)bar};
 }

 you should then sieve it with:

 ..
 int x;
 double y;
 {x, y} foo (20);
 ..

 Something like that?
What about : (int,double) foo (int bar ) { ... return( bar, (double)bar ); } ... (int x, double y ) = foo( 20 ); ... -- Nicolas Repiquet
Sep 30 2003
prev sibling parent "Sean L. Palmer" <palmer.sean verizon.net> writes:
Yes, a tuple is pretty much an anonymous struct with anonymous data members,
declared in place in ad-hoc fashion.

Yeah, I don't know why we can't put all the out variables as results though.
They're not exactly parameters, they're results.  Either that or make all
results into parameters.  Just keep it consistent.

Sean

"Sjoerd van Leent" <Sjoerd_member pathlink.com> wrote in message
news:bl9nu3$9m5$1 digitaldaemon.com...
 Tuples, I remember clean again. A tuple is not something more than a plain
 structure of some data, so why not use a struct for it?

 Besides that, a function calculates only one value as return value, being
it a
 struct, object or primitive, so if you want to use more out values you
should
 use a function like the following:

 void proc ( in int x, out int y, out int z ) { ... }
Sep 30 2003