Null object

From Rosetta Code
Revision as of 08:57, 21 March 2009 by rosettacode>Spoon! (added standard ml)
Task
Null object
You are encouraged to solve this task according to the task description, using any language you may know.

Null (or nil) is the computer science concept of an undefined or unbound object. Some languages have an explicit way to access the null object, and some don't.

Show how to access null in your language by checking to see if an object is equivalent to the null object.

ActionScript

<lang actionscript> if (object == null)

   trace("object is null");

</lang>

ActionScript also has a special undefined value which applies to untyped variables and properties of dynamic classes which have not been initialized. <lang actionscript> var foo; // untyped var bar:*; // explicitly untyped

trace(foo + ", " + bar); // outputs "undefined, undefined"

if (foo == undefined)

   trace("foo is undefined"); // outputs "foo is undefined"

</lang>

Ada

<lang ada>with Ada.Text_Io;

if Object = null then

  Ada.Text_Io.Put_line("object is null");

end if;</lang>

ALGOL 68

In ALGOL 68 the NIL yields a name that does not refer to any value. NIL can never be naturally coerced and can only appear where the context is strong.

REF STRING no result = NIL;
STRING result := "";

IF no result :=: NIL THEN print(("no result :=: NIL", new line)) FI;
IF result :/=: NIL THEN print(("result :/=: NIL", new line)) FI;

IF no result IS NIL THEN print(("no result IS NIL", new line)) FI;
IF result ISNT NIL THEN print(("result ISNT NIL", new line)) FI;

COMMENT using the UNESCO/IFIP/WG2.1 ALGOL 68 character set
  result := °;
  IF REF STRING(result) :≠: ° THEN print(("result ≠ °", new line)) FI;
END COMMENT

Note the following gotcha:

REF STRING var := NIL;
IF var ISNT NIL THEN print(("The address of var ISNT NIL",new line)) FI;
IF var IS REF STRING(NIL) THEN print(("The address of var IS REF STRING(NIL)",new line)) FI

Output:

no result :=: NIL
result :/=: NIL
no result IS NIL
result ISNT NIL
The address of var ISNT NIL
The address of var IS REF STRING(NIL)

NIL basically is an untypes REF (pointer) that does not refer anywhere.

ALGOL 68 also has EMPTY. This is a "constant" of size 0 and type VOID. c.f. Roots of a function for two different examples of usage.

  • EMPTY as an undefined argument to a routine.
  • EMPTY as a routine return if no result is found.

EMPTY is typically used to refer to am empty leaf in a tree structure.

Basically:

  • ALGOL 68's EMPTY is python's None,
  • ALGOL 68's VOID is python's NoneType, and
  • ALGOL 68's NIL is python's hash(None)

C

C's access to null is by way of a macro which simply evaluates to 0. <lang c>#include <stdio.h>

  1. include <stdlib.h>

if (object == NULL) {

  printf("object is null");

}</lang>

C++

C++'s access to null is (as in C) by way of a macro which simply evaluates to 0. <lang cpp>#include <iostream>

  1. include <cstdlib>

if (object == NULL) {

  std::cout << "object is null";

}</lang>

C#

As with Java, any reference type may be null, and testing for nullity uses ordinary boolean operators. <lang csharp> if(testObject==null) {

   Console.WriteLine("foo is null");

} </lang>

C# 2.0 introduced nullable types for situations in which even primitive value types may have undefined or unknown values (for example, when reading from a database). Prior to the introduction of nullable types, these situations would require writing wrapper classes or casting to a reference type (e.g., object), incurring the penalties of boxing and reduced type safety. A variable with nullable type can be declared simply by adding the '?' operator after the type.

Works with: C# version 2.0+

<lang csharp> int? x = 12; x = null; </lang>

Also new in C# 2.0 was the null coalescing operator, '??', which is simply syntactic sugar allowing a default value to replace an operand if the operand is null:

Works with: C# version 2.0+

<lang csharp> Console.WriteLine(name ?? "Name not specified");

//Without the null coalescing operator, this would instead be written as: //if(name == null){ // Console.WriteLine("Name not specified"); //}else{ // Console.WriteLine(name); //} </lang>

D

Library: tango

In D is is used for comparing identity, like two objects or an object against null. <lang d>import tango.io.Stdout;

if(object is null) {

  Stdout.formatln("object is null");

}</lang>

E

object == null

Haskell

Haskell does not have a universal null value. There is a 'value of every type', the undefined value (sometimes written ⊥, 'bottom'), but it is essentially a sort of exception — any attempt to use it is an error.

undefined      -- undefined value provided by the standard library
error "oops"   -- another undefined value
head []        -- undefined, you can't take the head of an empty list

When one would use "null" as a marker for "there is no normal value here" (e.g. a field which is either an integer or null), one uses the Maybe type instead. The definition of Maybe is:

data Maybe a = Nothing | Just a

That is, a Maybe Integer is either Nothing or Just <some integer>.

There are many ways to work with Maybe, but here's a basic case expression:

case thing of
 Nothing -> "It's Nothing. Or null, whatever."
 Just v  -> "It's not Nothing; it is " ++ show v ++ "."

Io

 if(object == nil, "object is nil" println)

Java

In Java, "null" is a value of every reference type. <lang java>// here "object" is a reference if (object == null) {

  System.out.println("object is null");

}</lang>

to test :thing
if empty? :thing [print [list or word is empty]]
end
print empty? []  ; true
print empty? "|| ; true

MAXScript

if obj == undefined then print "Obj is undefined"

Modula-3

In Modula-3, NIL is a value, and NULL is a type. The NULL type contains only one value, NIL. NULL is a subtype of all reference types, which allows all reference types to have the value NIL.

This can lead to errors, if for example you write: <lang modula3>VAR foo := NIL</lang> This (most likely incorrectly) gives foo the type NULL, which can only have the value NIL, so trying to assign it anything else will not work. To overcome this problem, you must specify the reference type when declaring foo: <lang modula3>VAR foo: REF INTEGER := NIL;</lang> <lang modula3>IF foo = NIL THEN

 IO.Put("Object is nil.\n");

END;</lang>

Objective-C

The value nil is used to indicate that an object pointer (variable of type id) doesn't point to a valid object. <lang objc>// here "object" is an object pointer if (object == nil) {

  NSLog("object is nil");

}</lang> An interesting thing is that in Objective-C, it is possible to send a message to nil, and the program will not crash (nothing will be executed; an exception is raised, but it does not interrupt the program). <lang objc>[nil fooBar];</lang>

Note that nil is distinct from NULL, which is only used for regular C pointers.

Confusingly, there is also NSNull, a singleton class with one value, [NSNull null], used as a dummy object to represent the lack of a useful object. This is needed in collections like arrays and dictionaries, etc., because they do not allow nil elements, so if you want to represent some "empty" slots in the array you would use this.

OCaml

Maybe the closest type of OCaml would be the type option, which is defined like this in the standard library:

type 'a option = None | Some of 'a
 match v with
 | None -> "unbound value"
 | Some _ -> "bounded value"

Perl

In Perl, all variables are undefined by default. The defined function returns true iff its argument is defined. Hence, this statement on its own will print "Undefined." <lang perl>print +(defined $x ? 'Defined' : 'Undefined'), ".\n";</lang>

PHP

There is a special value NULL. You can test for it using is_null() or !isset() <lang php>$x = NULL; if (is_null($x))

 echo "\$x is null\n";</lang>

Python

<lang python>x = None if x is None:

 print "x is None"

else:

 print "x is not None"

</lang>

Output:

x is None

Ruby

<lang ruby>if object == nil

  puts "object is null"

end</lang>

Scheme

<lang scheme>(null? object)</lang> Note: "null?" here tests whether a value is the empty list.

Standard ML

Maybe the closest type of Standard ML would be the type option, which is defined like this in the standard library:

datatype 'a option = NONE | SOME of 'a
case v of NONE => "unbound value"
        | SOME _ => "bounded value"