I fail to understand how to correctly assure that something is not nil
in this case:
package main
type shower interface {
getWater() []shower
}
type display struct {
SubDisplay *display
}
func (d display) getWater() []shower {
return []shower{display{}, d.SubDisplay}
}
func main() {
// SubDisplay will be initialized with null
s := display{}
// water := []shower{nil}
water := s.getWater()
for _, x := range water {
if x == nil {
panic("everything ok, nil found")
}
//first iteration display{} is not nil and will
//therefore work, on the second iteration
//x is nil, and getWater panics.
x.getWater()
}
}
The only way I found to check if that value is actually nil
is by using reflection.
Is this really wanted behaviour? Or do I fail to see some major mistake in my code?
The problem here is that
shower
is aninterface
type. Interface types in Go hold the actual value and its dynamic type. More details about this: The Laws of Reflection #The representation of an interface.The slice you return contains 2 non-
nil
values. The 2nd value is an interface value, a (value;type) pair holding anil
value and a*display
type. Quoting from the Go Language Specification: Comparison operators:So if you compare it to
nil
, it will befalse
. If you compare it to an interface value representing the pair(nil;*display)
, it will betrue
:This seems unfeasible as you'd have to know the actual type the interface holds.
Why is it implemented this way?
Interfaces unlike other concrete types (non-interfaces) can hold values of different concrete types (different static types). The runtime needs to know the dynamic or runtime-type of the value stored in a variable of interface type.
An
interface
is just a method set, any type implements it if the same methods are part of the method set of the type. There are types which cannot benil
, for example astruct
or a custom type withint
as its underlying type. In these cases you would not need to be able to store anil
value of that specific type.But any type also includes concrete types where
nil
is a valid value (e.g. slices, maps, channels, all pointer types), so in order to store the value at runtime that satisfies the interface it is reasonable to support storingnil
inside the interface. But besides thenil
inside the interface we must store its dynamic type as thenil
value does not carry such information. The alternate option would be to usenil
as the interface value itself when the value to be stored in it isnil
, but this solution is insufficient as it would lose the dynamic type information.In general if you want to indicate
nil
for a value ofinterface
type, use explicitnil
value and then you can test fornil
equality. The most common example is the built-inerror
type which is an interface with one method. Whenever there is no error, you explicitly set or return the valuenil
and not the value of some concrete (non-interface) type error variable (which would be really bad practice, see demonstration below).In your example the confusion arises from the facts that:
shower
)shower
but a concrete typeSo when you put a
*display
type into theshower
slice, an interface value will be created, which is a pair of (value;type) where value isnil
and type is*display
. The value inside the pair will benil
, not the interface value itself. If you would put anil
value into the slice, then the interface value itself would benil
and a conditionx == nil
would betrue
.Demonstration
See this example: Playground
Output:
In case 2 a
nil
pointer is returned but first it is converted to an interface type (error
) so an interface value is created which holds anil
value and the type*MyErr
, so the interface value is notnil
.Let's think of an interface as a pointer.
Say you have a pointer
a
and it's nil, pointing to nothing.Then you have a pointer
b
and it's pointing toa
.See what happened?
b
points to a pointer that points to nothing. So even if it's a nil pointer at the end of the chain,b
does point to something - it isn't nil.If you'd peek at the process' memory, it might look like this:
See?
a
is pointing to address 0 (which means it'snil
), andb
is pointing to the address ofa
(1000000).The same applies to interfaces (except that they look a bit different in memory).
Like a pointer, an interface pointing to a nil pointer would not be nil itself.
Here, see for yourself how this works with pointers and how it works with interfaces.