Live data from Hacker News

Were multiple return values Go's biggest mistake?

herecomesthemoon.net

1–10 of 25 posts

Re: Were multiple return values Go's biggest mistake?

#4
I think Go (as a better C) addressed one of C's weaknesses - only being able to return one value.

The C ABI is quite capable of it provided you dress it up into a struct, you just can't do it without defining that useless one off struct.

IMHO Go fixed that C wart.

If you look at the Go calling convention, multiple return values look perfectly natural. The parameters of a function are passed on the stack and the return values are returned immediately after on the stack . That's why naming your return values is just like naming your parameters - they are both local stack based variables.

So I would argue that multiple return values are great.

Maybe you should be able to treat all the return arguments as a tuple (eg Python). That would make certain things neater, but I think it is a different argument.

Re: Were multiple return values Go's biggest mistake?

#5
> I don’t want to sound mean, but a friend of mine has suggested that he believes that “Rob Pike invented Go as a practical joke.”, and things like this really make me wonder if he has a point. Why would you design a language where the result of a function call cannot be stored or passed around?

Somebody show this person CMake, because if they've never used it, the resulting blog post will be incredible.

(CMake has functions... with arguments... and no return values. At all. Everything has to be saved to variables. Their workaround is that you can set and clear variables in the parent scope...)

Re: Were multiple return values Go's biggest mistake?

#8
Another thing that bugs me: if you have a function argument then it is copied by value, right? But it is hard to tell if the whole value is copied or not.

- structures argument: the whole structure is copied (unless you passed a pointer to the struct)

- interface? depends if the implementation is structure or pointer.

- maps: it copies just a small internal struct that is pointing to the implementation of the map.

- arrays: depends if it is a slice then copy cost is small (again, similar to maps), however an array is fully copied.

... complicated.

Re: Were multiple return values Go's biggest mistake?

#9
post #4

I think Go (as a better C) addressed one of C's weaknesses - only being able to return one value. The C ABI is quite capable of it provided you dress it up into a struct, you just can't do it without defining that useless one off struct. IMHO Go fixed that C wart. If you look at the Go calling convention, multiple return values look perfectly natural. The parameters of a function are passed on the stack and the retur…

> The parameters of a function are passed on the stack and the return values are returned immediately after on the stack. That's why naming your return values is just like naming your parameters - they are both local stack based variables.

Why should the user of a language need to know anything about how values are passed to/returned from a function? Passing them on the stack, in registers, on the heap are definitely in the 'implementation detail' camp.

And named return values seems to me to be just bookkeeping and (admittedly a good form of) user convenience.

That being said, I do like the way python does it with tuples and automagic tuple unpacking. Even the C-API makes it super easy to do multiple return values.

Re: Were multiple return values Go's biggest mistake?

#10
According to The Principle of Least Surprise (Principle of Least Astonishment, POLA)[1] there is should be a symmetry: either a function receives a single input value, and returns a single output value, or it may receive multiple values and return multiple values, any other combination is problematic.

The only advantage to multiple inputs - single output, is that it's resembling the math notation, but it also inherits all of its shortcomings, and obviosly it wasn't designed with computers in mind.

Same with function taking or returning 0 values (e.g. void).

Function receiving 0 inputs should be a constant.

Function returning 0 outputs, is a procedure with a side-effect, and should be implemented using a different construct.

---

1. https://en.wikipedia.org/wiki/Principle_of_least_astonishmen...

Post reply on HN