I don't understand, through these examples, why the introduction of a name is necessary in order that an empty return statement be used.
The name doesn't even appear in the function. A name that is declared but not subsequently mentioned serves no purpose (or some side purpose/hack).
In the "NoNamedReturnValue" variant, the return type is still declared. The compiler could, from that type alone, infer that "return" means "return a representative default instance of that type".
That is to say, why can't Go programmers just have this:
// oi name removed:
func NamedReturnParams(i int) (/* oi */ objectInfo) {
if i == 1 {
// Do one thing
return // wee, allow this anyway!
}
if i == 2 {
// Do another thing
return
}
if i == 3 {
// Do one more thing still
return
}
// Normal return
return
}
Also, why can't the compiler just optimize away the "return Objectinfo {}" statements down to "return", if those really are equivalent.
The name doesn't even appear in the function. A name that is declared but not subsequently mentioned serves no purpose (or some side purpose/hack).
In the "NoNamedReturnValue" variant, the return type is still declared. The compiler could, from that type alone, infer that "return" means "return a representative default instance of that type".
That is to say, why can't Go programmers just have this:
Also, why can't the compiler just optimize away the "return Objectinfo {}" statements down to "return", if those really are equivalent.