Re: [go-nuts] Why is reflect.ValueOf(nil map).Interface() != nil?

2016-08-09 Thread Ian Lance Taylor
On Tue, Aug 9, 2016 at 9:48 AM, Sam Salisbury wrote: > > The update I suggest is to add the following sentence to the end of the > reflect.Value.IsNil documentation: > >> Likewise, if v was created by calling ValueOf on an initialised >> interface{} value with a nil value pointer j, v.IsNil will r

Re: [go-nuts] Why is reflect.ValueOf(nil map).Interface() != nil?

2016-08-09 Thread Sam Salisbury
Playground link for above code: https://play.golang.org/p/rTrkkzEjRl On Tuesday, 9 August 2016 17:48:22 UTC+1, Sam Salisbury wrote: > > Thanks for the explanation, > > The update I suggest is to add the following sentence to the end of the > reflect.Value.IsNil documentation: > > Likewise, if v w

Re: [go-nuts] Why is reflect.ValueOf(nil map).Interface() != nil?

2016-08-09 Thread Sam Salisbury
Thanks for the explanation, The update I suggest is to add the following sentence to the end of the reflect.Value.IsNil documentation: Likewise, if v was created by calling ValueOf on an initialised interface{} > value with a nil value pointer j, v.IsNil will return true, whereas j == > nil wi

Re: [go-nuts] Why is reflect.ValueOf(nil map).Interface() != nil?

2016-08-09 Thread Michael Jones
The interface is like an envelope. You “open it” to see what’s inside. Sending nothing (no envelope arrives) is not the same as sending an empty envelope (i.e., interface is non-nil, pointer within is nil). It never confused me, beyond my general confusion from thinking in similes and metaphor

Re: [go-nuts] Why is reflect.ValueOf(nil map).Interface() != nil?

2016-08-09 Thread Ian Lance Taylor
On Tue, Aug 9, 2016 at 6:23 AM, Sam Salisbury wrote: > > All this gets me thinking, is there any use case where this fact is useful? > (I.e. a nil-valued interface not being equal to nil via the == operator.) This has been discussed several times on the mailing list. An interface value == nil if

Re: [go-nuts] Why is reflect.ValueOf(nil map).Interface() != nil?

2016-08-09 Thread Sam Salisbury
Thanks! All this gets me thinking, is there any use case where this fact is useful? (I.e. a nil-valued interface not being equal to nil via the == operator.) Also, should the reflect.Value.IsNil documentation be updated? It doesn't mention this cas

Re: [go-nuts] Why is reflect.ValueOf(nil map).Interface() != nil?

2016-08-09 Thread Jakob Borg
2016-08-09 10:26 GMT+02:00 Sam Salisbury : > So, for purely academic reasons, what if I wanted to write a function to > look inside an interface value to determine if its value pointer is nil? > > I tried this, following from your simplified example Steven > https://play.golang.org/p/oG2aQzlKfc

Re: [go-nuts] Why is reflect.ValueOf(nil map).Interface() != nil?

2016-08-09 Thread Sam Salisbury
Ah, thanks, surprised I've never hit this issue before. So, for purely academic reasons, what if I wanted to write a function to look inside an interface value to determine if its value pointer is nil? I tried this, following from your simplified example Steven https://play.golang.org/p/oG2

Re: [go-nuts] Why is reflect.ValueOf(nil map).Interface() != nil?

2016-08-08 Thread Steven Blenkinsop
Perhaps for clarity, the trip through reflect is a red herring. All you need is to put the map in an interface: package main > > import ( > "fmt" > ) > > func main() { > var m map[string]interface{} > if m != nil { > panic("m != nil") > } > var i interface{} = m > if i == nil { > // I expect this.

Re: [go-nuts] Why is reflect.ValueOf(nil map).Interface() != nil?

2016-08-08 Thread Ian Lance Taylor
On Mon, Aug 8, 2016 at 10:07 AM, Sam Salisbury wrote: > The code speaks for itself, I thought I was understanding reflection up > until this point... It seems that the zero value of maps is nil, until you > round-trip them to a reflect.Value, and they become non-nil. This is an instance of https:

[go-nuts] Why is reflect.ValueOf(nil map).Interface() != nil?

2016-08-08 Thread Sam Salisbury
The code speaks for itself, I thought I was understanding reflection up until this point... It seems that the zero value of maps is nil, until you round-trip them to a reflect.Value, and they become non-nil. The code below behaves similarly for slices. package main import ( "fmt" "reflect" )