-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Description
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:
"if" ( Expression | InterfaceType) Block
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 as false if the interface is nil or if its value is nil.
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:
if !v {
// v is an interface with value nil or holds a nil pointer
// it is unusable
}It also attempts simplifying the (by some considered tedious) error handling (#21161). It would become:
if err {
// handle error
}However I'm unsure if this has any side effects which could cause confusion or wrong assumptions.