go: proposal: Go 2: remove the "if" keyword and use "err?!" to avoid error handling boilerplate
This proposal looks ridiculous, but I think it has some good points, so I post it here anyway.
The proposal proposes that the if-block
if boolCondition {
...
} else {
...
}
should be simplified as
boolCondition? {
...
} else {
...
}
More generally, condition values can be of any type, instead of boolean types only:
condValue? {
...
} else {
...
}
which is equivalent to the current if-block as:
if condValue != zeroValueOfTypeOfCondValue {
...
} else {
...
}
In other words, the if keyword becomes useless and condValue is equvalent to condValue != zeroValueOfTypeOfCondValue. This really makes the proposal look ridiculous, but I think, currently in Go 1, the if-block and switch-case-block have too much functionality overlapping. In other words, I think the if keyword is not very essential for Go. This is one reason of removing the if keyword.
The other reason of removing the if keyword is we can use the following line
err? !
to simplify
err ? {
return x, y, err
}
to avoid error handling boilerplate.
[update 1] To avoid breaking the current semicolon insertion rules, err? ! can be replaced with
err?;
or
err??
[update 2] Per Russ’s comment, condValue is equvalent to condValue != zeroValueOfTypeOfCondValue can be limited to boolean and error values only, or just excludes string values.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 52
- Comments: 22 (10 by maintainers)
Excessive simplicity may be a burden.
If we replace the
ifkeyword with the?syntax, we’d also replace theelsekeyword with:.We can’t realistically remove the
ifkeyword at this point. It would break too many existing Go programs and too much existing Go documentation.I would already be happy with making either nil falsy everywhere oder just for the error type.
if err { return err}This would already remove most of the error checking boilerplate and is very simple and straight forward. Go format can in addition force it to be in one line.
Even this would be simpler to write but not harder to read:
if err := Foo(); err { return err}This is stupid.
In the upcoming 1.13 release we do have
reflect.Value.IsZero(https://tip.golang.org/pkg/reflect/#Value.IsZero). Definitely more awkward to use than a builtin function.Note that if
is equivalent to
then values of type
errorand values of typeboolwill behave somewhat differently. For a value of typeerrorthe block will be executed if there is an error. For values of typeboolthe block will be executed if the value istrue. That is, with the typical meanings of those values, in one case we will execute the block if there is a problem, and in the other case we will execute the block if things are OK. This could be acceptable, but it’s worth considering.@rocket049 Thanks for the note. I encourage you to either explain your position, or just use the emojis on the first comment to express your opinion. Please see https://golang.org/wiki/NoPlusOne , although in this case I guess it’s really NoMinusOne. Thanks.