Null object: Difference between revisions
m (Moved to Basic learning cat) |
m (→{{header|D}}: Full Program) |
||
Line 29: | Line 29: | ||
Use operator == to compare two objects by value. |
Use operator == to compare two objects by value. |
||
<d>import std.stdio; |
<d>import std.stdio; |
||
void main() { |
|||
⚫ | |||
if (object is null) { |
|||
⚫ | |||
} |
|||
}</d> |
}</d> |
||
Revision as of 11:25, 2 September 2008
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>
C
C's access to null is by way of a macro which simply evaluates to 0. <c>#include <stdio.h>
- 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>
- include <cstdlib>
if (object == NULL) {
std::cout << "object is null";
}</cpp>
D
Use operator == to compare two objects by value. <d>import std.stdio; void main() {
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>
Logo
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>if x == None:
print "x is None"</python>
Ruby
if object == nil
puts "object is null"
end