-
Notifications
You must be signed in to change notification settings - Fork 17.6k
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
proposal: Go 2: improve if statement #26712
Comments
I am against this. Go being strict about all conditionals being booleans is one of my favorite features. Treating non-boolean values as As an alternative, how about just altering func nilValue() *string {
return nil
}
func main() {
var i interface{} = nilValue()
println(i)
} What if this just made The argument could probably be made that |
@gbbr are you suggesting that:
Both seem to be undesirable. @DeedleFake
It would be surprising if [1] and [2] printed different values. |
I don't think it would be that surprising. You're comparing an empty string to var i interface{} = ""
println(i == "") prints var i interface{} = (*struct{})(nil)
println(i == nil) Now, var i interface{} = (*struct{})(nil)
println(i == (*struct{})(nil)) does, but this seems almost like an oversight in the type system. It makes it look like a literal I think this complication primarily arises from the fact that Bottom line, both of the following print var i interface{} = float32(0)
println(i == 0)
i = (*struct{})(nil)
println(i == nil) Unfortunately, this may be more complicated than it seems, as it would require the equality to recognize that it's comparing an interface against a literal and handle the literal typing at runtime. |
It probably is the simplest way (someone else probably mentioned it, but the issue is gargantuan) to eliminate the |
If making the nil check shorter is the only reason for this if statement, maybe the following alternative would work (albeit with some reflection)? func nl(v interface{}) bool {
if v == nil {
return true
}
r := reflect.ValueOf(v)
if r.Kind() != reflect.Ptr {
return false
}
return r.IsNil()
} The above example would be if !nl(err) {
// handle error
} |
var x interface{} = false
[…]
if x {
// x == false, but here we are!
} |
Those two examples can't be compared. The first one compares Perhaps it would help to rewrites this example as:
Now it's clear why the first one prints We could change the meaning of ========================= Arguably, if code requires using a function like The current behavior of The following API means: "provide anything that I can call Foo on, or ask me
It is not uncommon to allow calling methods on nil (e.g. protocol
By changing the meaning of comparison with nil, as discussed, we'd That is, we lose the ability to express good APIs with simple code, |
This is in effect an implicit conversion from all interface values to |
This proposal is for considering the introduction of a new form to the
if
statement by allowing not only expressions, but also an interface. It would look like this:This would change the
if
statement's behavior in such a way that when an interface argument is provided (as opposed to an expression), the statement would evaluate it asfalse
if the interface isnil
or if its value isnil
.This could maybe solve the problem when newcomers assume wrongly about
err == nil
(see FAQ entry) and offer a good solution to performing this check (#22729). Example:It also attempts simplifying the (by some considered tedious) error handling (#21161). It would become:
However I'm unsure if this has any side effects which could cause confusion or wrong assumptions.
The text was updated successfully, but these errors were encountered: