-
Notifications
You must be signed in to change notification settings - Fork 18.1k
proposal: Go 2: make named return parameter assignment on last line a terminating statement of a function #31630
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is not going to be a popular proposal. There's been a lot of discussion about not even allowing that last example, as it's very easy to write buggy code with it, even with the |
@DeedleFake What |
The following code fails to compile: func example(t int) (v string) {
if t > 3 {
v := 3
return
}
return
} |
sure it does not compile, because it is wrong |
ok that is a valid argument but it depends also on your viewpoint.
If you consider a function to be a lambda expression then there are just input arguments and output arguments but not necessarily a return. Why treat input arguments different than the output arguments.
or
or
No you would not want to write your functions like that. But then what is the logic behind that "feeling". What is the reason (other then habit of previous millennium) to have an end-of-function indicator at the end of the function? Because I have the feeling that I write an assembler RET instruction at the end of the functions which feels to me totally out of place and out of style in a modern language like GO. Are we keeping that return statement just because we "feel" we have to keep it? And please do not suggest to drop the syntax to declare the output arguments with the input arguments (because that was a great idea and I love the consistency that it introduces). |
I know. I was answering @randall77's question. And there are plenty of reasons to redeclare a variable with the same name. The most common, probably, is if you have a named func Example(q bool) (err error) {
if q {
file, err := os.Open("file.txt")
if err != nil {
// The compiler will catch this for you, even though there's
// technically nothing wrong with the code, simply because
// it's highly likely that it's an error.
return
}
defer file.Close()
}
// Do something else...
} |
What if there are multiple result parameters? An explicit |
Please read your answer again. The result parameters are ALWAYS the actual result, EVEN if you do not assign a value to them! (just try it) Your reasoning is based on a mirage. It is obvious that you have NEVER used the new go syntax with return variables. |
Please be polite. There is a distinction between the result parameters and the values returned by a function. You can see this in code like func F() (x, y int) {
x, y = 1, 2
return 3, 4
} That function returns I agree that the |
I never tried that (that should not even be possible).
|
Also consider Kotlin,Swift,Rust that all have implicit return these days while you stick to the assembler past |
I'm confused. What exactly are you objecting to? Because you can rely on that. Unless you're worried about a |
I am objecting against the lack of logic that dictates that I have to say to return to the caller at the end of the function. To me that makes as little sense as that I would have to write "enter" at the start of each function to tell the compiler that that is the place to enter the function. |
I believe the intent is that all paths leaving a function (except those functions returning no values) should explicitly say what it wants the results to be. It either says I can see your argument. But we've discussed this as far back as issue #320 and decided to go with requiring the explicit signal that the named results should stand. |
@StephanVerbeeck I apologize if it seems that we are dismissing your insight quickly. We thought about this issue many years ago and explicitly decided that we want it to work as it currently does (thanks to @randall77 for digging up #320, which I forgot about long ago). The "change that introduced output arguments next to input arguments" was made in the earliest stages of language development, well before the open source release in 2009, so this is not new.
The same is true of us. In case you didn't know, I personally have been working on Go full time since 2008. I say this not to imply that I am always right, or that I even know what I am talking about, but simply to say that I'm not making decisions casually or lightly. I should note that when I closed the issue above I was speaking not just for myself, but for the group of people who consider Go language changes. I apologize for not making that clear. |
I see how things are done. This is how C# came into existence, java community not accepting changes and microsoft walked away from the table. Then when C# overclassed JAVA and finally the JAVA community itself was tired of it and developed themselves Kotlin. The same frustration out of which GO was born, exists now against the people freezing it into a death standard. |
I'm sorry you feel frustrated. Every language decision has costs and benefits. There are rarely clear decisions. That said, this particular issue is only a question of whether a naked |
I don't agree with that, and even some of the argument you used is obviously wrong.
|
I don't agree that the reasoning on #320 was flawed.
The fact that most people don't care about this issue does not mean that there is no Go community. See an issue that people care about, such as #15292 or #19412.
People do in practice use result variable names as documentation, among other things. If this causes the compiler to generate additional code, then perhaps that is a bug to be fixed. I don't see a clear reason to change the language for this. That said, can you point us to the multiple times that users have demanded this change?
I'm not sure I understand. You can write a one line function using a
Is this the same as point 3?
But people really do name result parameters for documentation purposes, and that is not wrong.
The same rules apply. There is no compiler error for naming an input argument and not using it.
I closed the issue because we are not going to change the language as you suggest. Please do not make an unfair accusation like saying that it is because I do not want to do the work. If we thought that the language change was acceptable, and we didn't want to do the work, we would simply leave the issue open for others to work on. |
1 = mine That the same conclusion was reached twice should be a clue that the argument has a valid foundation.
That is correct but actually not what I intended to imply. My failure in communicating my idea is that I assume we have a common background, but now I see I will have to build up my argument more elaborate and clearly. So let me take my argument buildup step-by-step. There are 3 places where we can put a function argument in go:
the functions size1,size2,size3 are identical but size3 does not compile unless I write is as follows
The core of my reasoning is that there are multiple places where I can declare my output argument. Why is it that the most recent programming language is still locked in this assembler-age concept? So what I fail to grasp is how the introduction of named output arguments at the same time did NOT make the old-style return statement optional. Now it is a mix of two concepts neither of which is consistent. Please accept my proposal as language change to make the return statement OPTIONAL. I have looked at all the other issues that were referred here and did not found a spot where this discussion was held or argued. So yes maybe there was an issue with identical text long ago but that does not mean that the issue was properly addressed at that time. Closing this issue should have been accompanied by an argument or reference to an argument but I see neither. I accept that the same issues can not be repeated over and over (given the number of open issues) but, unless it is given proper attention it will remain a structural weakness in the language. |
I just realized that the return statement is ALREADY optional.
|
I'm glad that you have found an approach that can work for you, even though it is imperfect. |
hmmm to be honest I have more serious concerns for the moment. In an hour we have a staff meeting about canceling the entire multi platform development (windows, mac, iOS, android) of PilotSpace. |
If you have benchmarks that demonstrate these performance problems, please open issues including your benchmark code so we can take a look. |
I can not spend time on this anymore but feel free to replicate the test #32195 |
Consider the following 2 alternatives for implementing the same function that returns a result value. The second syntax is my favorite and I use it throughout my code (I am currently developing a commercial accounting application in GOlang (based on lxn/walk)).
But when using the predefined variable it seems very illogical to me that GO complains about leaving out the final return statement so I must correct this by doing this:
My proposal is to NOT generate the "missing return at end of function" error in the latter case allowing programmers to leave out this obviously superfluous line of code.
The text was updated successfully, but these errors were encountered: