Multiple return values...

Manu turkeyman at gmail.com
Tue Mar 13 08:48:26 PDT 2012


On 13 March 2012 16:44, Andrei Alexandrescu
<SeeWebsiteForEmail at erdani.org>wrote:

> I thought more about it and we should be fine with two functions
> (untested):
>
> enum Skip {};
> @property ref Skip skip() {
>    static __gshared Skip result;
>    return result;
> }
>
> void scatter(T, U...)(auto ref T source, ref U targets) {
>    assert(source.length == targets.length);
>    foreach (i, ref target; targets) {
>        static if (is(typeof(target) != Skip)) {
>            target = source[i];
>        }
>    }
> }
>
> void gather(T, U...)(ref T target, auto ref U sources) {
>    assert(target.length == sources.length);
>    foreach (i, source; sources) {
>        static if (is(typeof(source) != Skip)) {
>            target[i] = source;
>        }
>    }
> }
>
> Usage:
>
> auto t = tuple(1, "hi", 2.3);
> int a;
> string b;
> double c;
> t.scatter(a, b, skip); // assigns a and b from tuple
> b = "!";
> ++c;
> t.gather(skip, b, c); // assigns tuple from variables b and c


Well, that 'works' :) .. Is that a proposal for a 'final' syntax, or
something to work with in the mean time?
I said I've come to accept the Tuple *implementation*, but I'm absolutely
not ready to accept the syntax baggage ;)
I'd really rather see something that actually looks like a language feature
in its final manifestation. Is natural and convenient to read and type.

float t;
...
(myStruct.pos, t, _, int err) = intersectThings();

Or something to this effect. That's about as clear and concise as it gets
for my money.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20120313/bacd2cc7/attachment.html>


More information about the Digitalmars-d mailing list