Code example:
http://play.golang.org/p/0RQNesP2IC
I understand that struct data is separated from struct methods, but I couldn't think of any practical reasons where this would be a good idea and not indicate a bug. It seems like it could cause strange problems or really bad style (using structs as a poor-man's package).
That seems to me like a programming mistake, and the philosophy is to panic on programmer error (most likely).
Have you encountered this scenario in real code? I'm curious for more context.
Andrew
--
On Sep 12, 2012 6:40 AM, "roger peppe" <rogp...@gmail.com> wrote:
> I think of my proposed change as a little like the change
> that made nil maps work as if they were empty maps,
> and nil channels work but always block - i.e. it doesn't
> interfere and it makes some kinds of code easier to write.
FWIW, as a newbie, I recently spent at least an hour trying to figure out why my project developed a hanging issue before I found that I hadn't run make on two of my newer channels.
That kind of behavior only doesn't interfere if you remember to do everything properly; I would have preferred that it panic :-)
Method dispatch, as is used in some other languages with something like objectpointer->methodName() involves inspection and indirection via the indicated pointer (or implicitly taking a pointer to the object) to determine what function to invoke using the named method. Some of these languages use pointer dereferencing syntax to access the named function. In any such cases, calling a member function or method on a zero pointer understandably has no valid meaning.In Go, however, the function to be called by the Expression.Name() syntax is entirely determined by the type of Expression and not by the particular run-time value of that expression, including nil. In this manner, the invocation of a method on a nil pointer of a specific type has a clear and logical meaning. Those familiar with vtable[] implementations will find this odd at first, yet, when thinking of methods this way, it is even simpler and makes sense. Think of:func (p *Sometype) Somemethod (firstArg int) {}as having the literal meaning:func SometypeSomemethod(p *Sometype, firstArg int) {}and in this view, the body of SometypeSomemethod() is certainly free to test it's (actual) first argument (p *Sometype) for a value of nil. Note though that the calling site invoking on a nil value must have a context of the expected type. An effort to invoke an unadorned nil.Somemethod would not work in Go because there is be no implicit "Sometype" for the typeless value nil to expand the Somemethod() call into "SometypeSomemethod()"
Warning: Stream of consciousness email from the Sydney airport. May make no sense. Quite tired.