Null object

From Rosetta Code
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

<actionscript> if (object == null)

   trace("object is null");

</actionscript>

ActionScript also has a special undefined value which applies to untyped variables which have not been initialized. <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"

</actionscript>

Ada

<Ada>with Ada.Text_Io;

if Object = null then

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

end if;</Ada>

ALGOL 68

In ALGOL 68 the NIL yields a name that does not refer to any value. NIL can never be 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 REF STRING(var) IS NIL THEN print(("The REF STRING var IS NIL",new line)) FI

Output:

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

NIL basically is a generic 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 None.hash()

C

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

  1. include <stdlib.h>

if (object == NULL) {

  printf("object is null");

}</c>

C++

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

  1. include <cstdlib>

if (object == NULL) {

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

}</cpp>

C#

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

   Console.WriteLine("foo is null");

} </csharp>

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+

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

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+

<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); //} </csharp>

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. <java>// here "object" is a reference if (object == null) {

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

}</java>

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"

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." <perl>print +(defined $x ? 'Defined' : 'Undefined'), ".\n";</perl>

PHP

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

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

Python

<python>x = None if x is None:

 print "x is None"

else:

 print "x is not None"

</python>

Output:

x is None

Ruby

if object == nil

  puts "object is null"

end

Scheme

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