I normally hate it when people immediately trot out the old "premature optimization" quote, but it really applies here. Please don't go around naming all your returns just because today's compiler happens to generate better code with them. This is a compiler issue that I'm confident will be fixed one day, especially if you do the right thing and file an issue. But by all means, if you're profiling and your inner loop…
Benefits of named return values in Go
11–20 of 146 posts
Re: Benefits of named return values in Go
#12go fix?
Also, it seems like this is begging for a compiler optimization that will make this all obsolete...
Re: Benefits of named return values in Go
#13Re: Benefits of named return values in Go
#14I was going to post "But JS has this: return { named_var_1, named_var_2 } const { named_var_1, named_var_2 } = f() But then I read the article. I don't know really what is going on in this article on a quick reading, but it builds by existing sense that go is a really cool language that I ought to get into. I have no idea if anyone wants to help me understand, but in the last part where he changes to use a named retu…
Returning a new unnamed variable for every return will cause all of the unnamed variables to be allocated.
It's interesting, I don't use this feature of the language... my default way to write this would have been:
func NoNamedReturnParams(i int) (*objectInfo) {
obj := &objectInfo{}
if i == 1 {
// Do one thing
return obj
}
if i == 2 {
// Do another thing
return obj
}
if i == 3 {
// Do one more thing still
return obj
}
// Normal return
return obj
}Re: Benefits of named return values in Go
#15I normally hate it when people immediately trot out the old "premature optimization" quote, but it really applies here. Please don't go around naming all your returns just because today's compiler happens to generate better code with them. This is a compiler issue that I'm confident will be fixed one day, especially if you do the right thing and file an issue. But by all means, if you're profiling and your inner loop…
We should just fix the compiler.
Re: Benefits of named return values in Go
#16Re: Benefits of named return values in Go
#17I was going to post "But JS has this: return { named_var_1, named_var_2 } const { named_var_1, named_var_2 } = f() But then I read the article. I don't know really what is going on in this article on a quick reading, but it builds by existing sense that go is a really cool language that I ought to get into. I have no idea if anyone wants to help me understand, but in the last part where he changes to use a named retu…
It allows you to assign to the "return variables" inside the function, then just return without naming all the returned values at every return. e.g. https://play.golang.org/p/gdac5QR-wW This can also be used to return the default value for a type by never assigning anything to the return value.
https://play.golang.org/p/7bfiSqKY_w
I was also looking for a "spread" / ... operator, but couldn't see a way to do that.
Re: Benefits of named return values in Go
#18Re: Benefits of named return values in Go
#19I was going to post "But JS has this: return { named_var_1, named_var_2 } const { named_var_1, named_var_2 } = f() But then I read the article. I don't know really what is going on in this article on a quick reading, but it builds by existing sense that go is a really cool language that I ought to get into. I have no idea if anyone wants to help me understand, but in the last part where he changes to use a named retu…
Naming the return value in the function signature will both allocate the named variable, as well as make it the default variable to return. Returning a new unnamed variable for every return will cause all of the unnamed variables to be allocated. It's interesting, I don't use this feature of the language... my default way to write this would have been: func NoNamedReturnParams(i int) (*objectInfo) { obj := &objectInf…
It really hammers home the concept that a function is a transformation, and of what into what. And I think this syntax would probably encourage pure functions. And it's so useful to allocate the return in the top line. I really like go.
Re: Benefits of named return values in Go
#20I normally hate it when people immediately trot out the old "premature optimization" quote, but it really applies here. Please don't go around naming all your returns just because today's compiler happens to generate better code with them. This is a compiler issue that I'm confident will be fixed one day, especially if you do the right thing and file an issue. But by all means, if you're profiling and your inner loop…
I dunno. A 30% code size win is non-trivial. I'm all for filing an issue first and seeing how likely it is that there is uptake from the dev team and desire to fix the problem. However, if no fix is forthcoming . . . code size has fairly well known effects on performance.
Having said that, this is certainly something that should be fixed in the compiler.
On a related note, in the final assembly, the compiler could also have optimized the 4 RETs into 1, then optimized away all of the conditionals, turning the sample code into the equivilent of "return objectInfo()"). Of course, in a real example, these optimizations would not be possible; but they do show that these reduced cases are not the best way of benchmarking performance.