Null object: Difference between revisions
(add Haskell example) |
(Logo) |
||
Line 47: | Line 47: | ||
System.out.println("object is null"); |
System.out.println("object is null"); |
||
}</java> |
}</java> |
||
=={{header|Logo}}== |
|||
to test :thing |
|||
if empty? :thing [print [list or word is empty]] |
|||
end |
|||
print empty? [] ; true |
|||
print empty? "|| ; true |
|||
=={{header|MAXScript}}== |
=={{header|MAXScript}}== |
Revision as of 05:17, 7 August 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>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<stdio>
if(object == NULL){
cout << "object is null";
}</cpp>
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 ++ "."
Java
<java>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"
Python
<python>if x == None:
print "x is None"</python>
Ruby
if object == nil
puts "object is null"
end