Null object

From Rosetta Code
Revision as of 18:53, 8 September 2009 by rosettacode>DanBron (added)
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)

AmigaE

<lang amigae>DEF x : PTR TO object -> ... IF object <> NIL

 -> ...

ENDIF</lang>

AppleScript

Many applications will return missing value, but null is also available. <lang AppleScript>if x is missing value then

 display dialog "x is missing value"

end if

if x is null then

 display dialog "x is null"

end if </lang>

AutoHotkey

<lang AutoHotkey> If (object == null)

 MsgBox, object is null

</lang>

C

C's access to null is by way of a macro which simply evaluates to (void*) 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 (foo == 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>

Common Lisp

nil is in fact the only false value. So a regular Boolean test will suffice to determine whether an object is nil.

<lang lisp>(format t "The object is~A nil"

   (if some-object " not" ""))</lang>

There is also the function null which tests whether some value is nil; it is equivalent to not but for the intent conveyed.

On the other hand, a symbol bound to nil is distinct from a symbol without any bindings. Trying to read the value of unbound symbol is an error of type unbound-variable. You can test whether a symbol is globally bound with boundp.

<lang lisp>(format t "The symbol is~A bound"

   (if (boundp 'some-object) "" " not"))</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

Forth

Standard ANS Forth does not distinguish a particular invalid memory value like NULL. Instead, ALLOCATE returns an out-of-band success code to indicate a failed allocation. Dictionary words have the option of throwing an exception on a dictionary space overrun. Forth lacks a NULL symbol because it has such a wide variety of target platforms. On some embedded targets, the memory space may be as small as 64 direct-mapped addresses, where eliminating a valid zero address would have a high price.

In practice, all notable hosted implementations follow the C practice of being able to treat a zero address (i.e. FALSE) as a null address for the purpose of list termination.

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.

<lang haskell> 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</lang>

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:

<lang haskell> data Maybe a = Nothing | Just a</lang>

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:

<lang haskell> case thing of

 Nothing -> "It's Nothing. Or null, whatever."
 Just v  -> "It's not Nothing; it is " ++ show v ++ "."</lang>

Io

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

J

J doesn't have NULL. To indicate "missing data", "normal" data is usually pressed into service (e.g. 0 or _1 in a numeric context, ' ' in a literal context, a: in a boxed context, etc). Most frequently, missing data is represented by the empty vector .

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>

JavaScript

<lang javascript>if (object == null)

 alert("object is null")</lang>

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

Mathematica

Mathematica can assign a Null value to a symbol, two examples: <lang Mathematica>

x=Null;

</lang> <lang Mathematica>

x =.
x = (1 + 2;)
FullForm[x]

</lang> Both set x to be Null. To specifically test is something is Null one can use the SameQ function (with infix operator: ===): <lang Mathematica>

SameQ[x,Null]

</lang> Or equivalent: <lang Mathematica>

x===Null

</lang> will give back True if and only if x is assigned to be Null. If x is empty (nothing assigned) this will return False. To test if an object has something assigned (number, list, graphics, null, infinity, symbol, equation, pattern, whatever) one uses ValueQ: <lang Mathematica>

x =.;
ValueQ[x]
x = 3;
ValueQ[x]

</lang> gives: <lang Mathematica>

False
True

</lang>

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.

For class pointers (values of type Class), they have a separate null pointer value called Nil.

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: <lang ocaml> type 'a option = None | Some of 'a </lang> <lang ocaml>

match v with
| None -> "unbound value"
| Some _ -> "bounded value"

</lang>

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>

PowerShell

In PowerShell the automatic variable $null represents a null value. <lang powershell>if ($object -eq $null) {

   ...

}</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

R

R has the special value NULL to represent a null object. You can test for it using the function is.null. Note that R also has a special value NA to represent missing or unknown values. <lang R>

is.null(NULL)       # TRUE
is.null(123)        # FALSE
is.null(NA)         # FALSE
123==NULL           # Empty logical value, with a warning
foo <- function(){} # function that does nothing
foo()               # returns NULL

</lang>

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.

Slate

<lang slate> Nil isNil = True. </lang>


Smalltalk

<lang smalltalk>object isNil ifTrue: [ "true block" ]

            ifFalse: [ "false block" ].

nil isNil ifTrue: [ 'true!' displayNl ]. "output: true!"</lang>

Standard ML

Maybe the closest type of Standard ML would be the type option, which is defined like this in the standard library: <lang sml> datatype 'a option = NONE | SOME of 'a </lang> <lang sml> case v of NONE => "unbound value"

       | SOME _ => "bounded value"

</lang>

Tcl

In Tcl, where every value is a string, there is no out-of band value corresponding to NULL. In many cases, using the empty string is sufficient: <lang Tcl>if {$value eq ""} ...</lang> A stricter approximation to NULL can be had with non-existing variables or elements of a dict or array: <lang Tcl>if ![info exist nullvar] ... if ![info exists arr(nullval)] ... if ![dict exists $dic nullval] ...</lang>