Null object: Difference between revisions

From Rosetta Code
Content added Content deleted
Line 12: Line 12:
REF STRING x := NIL;
REF STRING x := NIL;
IF REF STRING(x) :=: NIL THEN
IF REF STRING(x) :=: NIL THEN print(("x := NIL", new line)) FI;
print(("x := NIL", new line))
IF REF STRING(x) :/=: NIL THEN print(("x :/=: NIL", new line)) FI;
ELIF REF STRING(x) :/=: NIL THEN
print(("x :/=: NIL", new line))
FI;
IF REF STRING(x) IS NIL THEN
IF REF STRING(x) IS NIL THEN print(("x IS NIL", new line)) FI;
print(("x IS NIL", new line))
IF REF STRING(x) ISNT NIL THEN print(("x ISNT NIL", new line)) FI;
ELIF REF STRING(x) ISNT NIL THEN
print(("x ISNT NIL", new line))
FI;
COMMENT using the UNESCO/IFIP/WG2.1 ALGOL 68 character set
COMMENT using the UNESCO/IFIP/WG2.1 ALGOL 68 character set
REF STRING x := °;
REF STRING x := °;
IF REF STRING(x) :≠: ° THEN
IF REF STRING(x) :≠: ° THEN print(("x ≠ °", new line)) FI;
print(("x ≠ °", new line))
FI;
END COMMENT
END COMMENT
# Note the following gotcha #
# Note the following gotcha #
IF x ISNT NIL THEN
IF x ISNT NIL THEN print(("The address of the pointer to NIL ISNT NIL",new line)) FI
print(("The address of the pointer to NIL ISNT NIL",new line))
FI
Output:
Output:
<pre>
<pre>
Line 129: Line 119:


=={{header|Python}}==
=={{header|Python}}==
None
<python>x = None
if x is None:

<python>if x is None:
print "x is None"
print "x is None"</python>
if x is not None:
print "x is not None"

</python>
Output:<pre>
x is None
</pre>
=={{header|Ruby}}==
=={{header|Ruby}}==
<ruby>if object == nil
<ruby>if object == nil

Revision as of 11:25, 20 October 2008

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.

Ada

<Ada>with Ada.Text_Io;

if Object = null then

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

end if;</Ada>

ALGOL 68

REF STRING x := NIL;

IF REF STRING(x) :=: NIL THEN print(("x := NIL", new line)) FI;
IF REF STRING(x) :/=: NIL THEN print(("x :/=: NIL", new line)) FI;

IF REF STRING(x) IS NIL THEN print(("x IS NIL", new line)) FI;
IF REF STRING(x) ISNT NIL THEN print(("x ISNT NIL", new line)) FI;

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

# Note the following gotcha #
IF x ISNT NIL THEN print(("The address of the pointer to NIL ISNT NIL",new line)) FI

Output:

x :=: NIL
x IS NIL
The address of the pointer to NIL ISNT NIL

Patent application: On 14 May 2003, software patent application No. 20040230959 "IS NOT OPERATOR" - US patent application 20040230959 was filed for the ISNOT operator by employees of Microsoft. This patent was granted on 18 November 2004.

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>

D

Use operator == to compare two objects by value. <d>import std.stdio; if (object is null) {

   writefln("object is null");

}</d>

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"

if x is not None:

 print "x is not None"

</python>

Output:

x is None

Ruby

if object == nil

  puts "object is null"

end

Scheme

<scheme>(null? object)</scheme>