Talk:Null object

From Rosetta Code

The C anc C++ examples don't really fit the task description (and it's not possible to do so, because C and C++ don't know a generic null object). What you can have is a null pointer, which is a pointer which doesn't point to any object (and that is indeed what NULL is for). The closest to a null object in C++ would be an empty boost::optional from the boost library.

Note that comparison to NULL may compile for non-pointer objects, but will in general not have the desired semantics: If NULL is defined as 0 (possible in C, more or less required in C++), then comparing any numeric object (or any other object with conversion from or to int) will actually compare the value of the object with zero.

Strictly speaking the Java example is incorrect as well (it doesn't test for a null object, but for a null reference), but since in Java objects are always accessed trough references, this distinction is probably mostly academic (note, however, that built-in fundamental types are not objects in the Java sense, and the null test isn't possible for them).

I don't know Ada, but I suspect the Ada example to be incorrect as well. --Ce 12:53, 5 August 2008 (UTC)

I think that insofar as this is a problem, we should fix it by changing the task, not the examples. The basic idea of null is similar enough for this collection to be informative, whether the language has everything-is-a-reference-including-null (Python), maybe-null references and primitives (Java, C), type systems which can include or exclude null (Common Lisp, E, C#), or data structures which explicitly add a case (Haskell) --Kevin Reid 22:42, 5 August 2008 (UTC)

I agree with Kevin, but I'm not sure how it should be reworded to make everyone happy without changing the examples. Any suggestions? --Mwn3d 00:06, 6 August 2008 (UTC)
I also agree. The problem with Ada's null and C++'s 0/NULL is that neither is the object. They are values of a reference type. I cannot tell what was the intention of the task, thinkable interpretations:
  • An [ideal/imaginary/exceptional] object substitutable for anything. "Null object" sounds like an instance of the root type, the common ancestor of all types, provided that the language has such a type. In which sense it were "null"? That is unclear because even the root type usually has some methods (like sizeof, ==, etc). An object that has methods, which incidentally propagate exceptions when called, does not qualify as "null", does it? Otherwise, to have a "null object" would be enough to create a type with one method that always raise some exception.
  • Like above, but a value. null is such thing for reference types.
  • Unbound name. That could be a forward declaration, a deferred constant, external object (to be linked to) etc, which is left unbound until program crashes attempting to access the corresponding object (= a language design fault). Not very promising.
  • Anything else?
--Dmitry-kazakov 08:05, 6 August 2008 (UTC)