Goは意図的に関数引数の一部として多値の戻り値を渡せなくしている
Goでは、関数の戻り値で多値を返すことができ、それを同じ数で同じ型の関数であればそのまま渡すことができる。
func Atoi(s string) (int, error)
func Must(n int, err error) int
n := Must(Atoi("34"))しかし、これを引数の一部として扱うことはできない。
fmt.Print("Prefix:", Atoi("34")) // これはできない// multiple-value Atoi("34") (value of type (int, error)) in single-value contextなぜかというと、Correctly pass multiple return values as arguments to other functionsスレッドのgriesemerが書いた返信に全てがあって、
To be clear, the current rules were carefully thought out and are deliberate. You can be assured that we thought about your suggestion during the design of the language (between 2007 and 2009). If your suggestion were permitted, what about
f(g(), h())where both g and h return multiple values that happen to match the parameter list of f? Should probably be permitted as well. And then we should also allow things like: “a, b, c, d := f(), g()” otherwise we have another inconsistency. And so forth. Permitting this is of course possible, but it also opens the door for unexpected errors. For instance, the parameter lists of the functions may change in unexpected ways and the code may still work, hiding errors. So the pragmatic solution here is to allow a small set of rules that are reasonably safe, and not to allow everything that might be possible.
- 部分的に使うことを認めると言語が必要以上に複雑化する
- 戻り値が増えた、または減った場合に気づかないバグが生じる可能性がある