Multiple return values...

Kevin Cox kevincox.ca at gmail.com
Fri Mar 9 07:47:57 PST 2012


On Mar 9, 2012 10:28 AM, "H. S. Teoh" <hsteoh at quickfur.ath.cx> wrote:
>
> On Fri, Mar 09, 2012 at 03:27:14PM +0100, Timon Gehr wrote:
> > On 03/09/2012 01:23 AM, Manu wrote:
> [...]
> > >int x; ... (x, float y) = func(); // assign to predeclared
variable(/s)?
> > >(x, , z) = func(); // ignore the second result value (elimination of
the
> > >second result's code path)
> > >
> >
> > Those two would work, but (x,y) = func(); conflicts with the comma
> > operator. (I'd prefer (,) to be a tuple constructor though.)
>
> Just out of curiosity, *why* does D have a comma operator? It's one of
> those obscure things about C that can be really, really, nasty if you're
> unaware of it. And C++ makes it worse by making the comma operator
> *overloadable

The comma operator can be worked around by using braces instead.  Of course
it is very breaking and quite arguably ugly.

for ({int I; float j;} ; ) ;

But then you have to worry about what it returns.  The sequence operator is
actually useful it is just unfortunate that they used it for function
parameters also.

I think that the best work around is either the braces which can be quite
elegant because it fits the rest of the language and say it returns the
return value of the last statement.  Or pick a new character for the
sequence operator.

This is actually kinds nice because you get "tuple" in "tuple" out.  Of
course they don't need to be tuples that can be passed around they can be
values that must be unpacked right away.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20120309/96cd418a/attachment.html>


More information about the Digitalmars-d mailing list