Re:Golang landmines
by thaumasiotes on Sunday September 18, 2016 @02:17AM
(#12524175)
What is the conceptual difference between a raw nil pointer, e.g. var t *Tabby = nil, and an interface with nil value and non-nil type? The raw pointer also has nil value and non-nil type.
[ Parent
| on HN ]
Re:Golang landmines
by Matthias247 on Sunday September 18, 2016 @04:21AM
(#12524411)
The difference is that on a nil-interface you can't call a method, it will panic. On a non-nil interface it will always call the method, wether a nil-pointer is stored as the receiver or not. It a nil pointer is stored it will call the method will it, and the method may still do something useful.
It can be theoretically be used to implement some interfaces without any backing object at all (singletons, pure functions, ...), which is a difference to most other programming languages.
Re:Golang landmines
by thaumasiotes on Sunday September 18, 2016 @04:25AM
(#12524421)
You're explaining the difference between two things, but not the two things I asked about.
Re:Golang landmines
by Matthias247 on Sunday September 18, 2016 @04:56AM
(#12524490)
Sorry - the question is not immediatly understandable. If you mean how 'var t *Tabby = nil' is different from the interface representation: For this one no type information is stored at runtime. It's only a single pointer. Whereas an interface at runtime is always represented by 2 fields, a pointer and a type field.
Re:Golang landmines
by thaumasiotes on Sunday September 18, 2016 @07:19AM
(#12524852)
Thanks. It didn't occur to me that non-interface types might not actually carry type information.