One document about Go

bearophile bearophileHUGS at lycos.com
Tue Jun 1 12:59:19 PDT 2010


Lurker:

>Combine the 2 things into one struct and return by value. Multi-value return makes for hard to comprehend code. So, IMO, it's hardly syntactic sugar: it's syntactic grafitti.<

You are very wrong, multiple return values are good, if they have a good enough syntax. Regarding this C/C++/D/Java/C#/etc are worse and Python and Go are better.

Multiple return values make code simpler to write, simpler to read and remove the need for silly and logically ridiculous features like "out" arguments (an input value that is a return? This is making things go backwards). D language isn't even able to tell if an "out" argument has being used before it is assigned inside the function (as C# compiler is able to, because in C# using uninitialized variables is an error) This is awful and surely worse than multiple return values.

import std.stdio: writeln;
import std.conv: to;

int foo(int x, out string s) {
    writeln(s);
    int result = x * x;
    s = to!string(result);
    return result;
}

void main () {
    string s;
    writeln(s);
    int r = foo(10, s);
    writeln(r);
}


A possible syntax for D multiple return values:


import std.stdio: writeln;
import std.conv: to;

(int, string) foo(int x) {
    int result = x * x;
    return (result, to!string(result));
}

void main () {
    (int r, string s) = foo(10);
    writeln(s);
    writeln(r);
}


The second is simpler to understand code and less bug-prone than the first.


An alternative syntax:

import std.stdio: writeln;
import std.conv: to;

[int, string] foo(int x) {
    int result = x * x;
    return [result, to!string(result)];
}

void main () {
    [int r, string s] = foo(10);
    writeln(s);
    writeln(r);
}

------------

Walter Bright:
> C got it right and it doesn't need fixing.

I have never programmed in Go, but keep in mind that Go designers are expert language designers and programmers. They aren't stupid people.

Anyway, this is not so important. Named arguments and multiple return values are quite more important (for D3).

Bye,
bearophile


More information about the Digitalmars-d mailing list