On Thu, Jan 16, 2014 at 11:00:24PM -0500, Michel Fortin wrote: > On 2014-01-17 01:42:37 +0000, Andrei Alexandrescu > <seewebsiteforem...@erdani.org> said: > > >Walter and I were talking today about the null pointer issue and > >he had the following idea. > > > >[...] > > > >Thoughts? > > Which null pointer issue were you discussing exactly? > > The one I'm mostly concerned about is the propagation of a null > value from point A to point B in a program, where you only detect > the null value at point B through a null dereference making it hard > to figure out from where this unexpected null value comes from. > Replace 'null' with 'sentinel' and it's no easier to figure out > where the invalid value comes from. Except now instead of checking > for null you have to check for null *and* for T.init *and* for all > the various Sublcass.init you might get. > > The idea is interesting, but I see it creating more problems than it > would solve. I'm not even sure I understand what problem it tries to > solve. [...]
AFAICT the null object pattern is intended to prevent dereferencing a null pointer (i.e., prevent a segfault on Posix), by providing a dummy object that has no-op stubs for methods and default values for fields. But from what I've been observing, what people have been asking for on this forum has been more a way of *preventing* null (or a sentinel, as here) from being assigned to a reference to begin with. So as far as that is concerned, this proposal doesn't really address the issue. Now, if we modify this sentinel to instead record the location of the code that first initialized it (via __FILE__ and __LINE__ default parameters perhaps), then we can set it up to print out this information at a convenient juncture, so that the source of the uninitialized reference can be determined. *Then* perhaps it will be a start of a solution to this issue. (Though it still has limitations in the sense that the problem can only be caught at runtime, whereas some cases of null dereference preferably should be caught at compile-time.) T -- Music critic: "That's an imitation fugue!"