Null object: Difference between revisions

Content deleted Content added
m →‎{{header|Ruby}}: use nil? method
m Fixed lang tags.
Line 4: Line 4:


=={{header|ActionScript}}==
=={{header|ActionScript}}==
<lang actionscript>
<lang actionscript>if (object == null)
if (object == null)
trace("object is null");</lang>
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.
ActionScript also has a special '''undefined''' value which applies to untyped variables and properties of dynamic classes which have not been initialized.
<lang actionscript>
<lang actionscript>var foo; // untyped
var foo; // untyped
var bar:*; // explicitly untyped
var bar:*; // explicitly untyped


Line 17: Line 14:


if (foo == undefined)
if (foo == undefined)
trace("foo is undefined"); // outputs "foo is undefined"
trace("foo is undefined"); // outputs "foo is undefined"</lang>
</lang>


=={{header|Ada}}==
=={{header|Ada}}==
Line 48: Line 44:
IF var IS REF STRING(NIL) THEN print(("The address of var IS REF STRING(NIL)",new line)) FI
IF var IS REF STRING(NIL) THEN print(("The address of var IS REF STRING(NIL)",new line)) FI
Output:
Output:
<lang algol68>no result :=: NIL
<pre>
no result :=: NIL
result :/=: NIL
result :/=: NIL
no result IS NIL
no result IS NIL
result ISNT NIL
result ISNT NIL
The address of var ISNT NIL
The address of var ISNT NIL
The address of var IS REF STRING(NIL)
The address of var IS REF STRING(NIL)</lang>
</pre>
NIL basically is an untypes REF (pointer) that does not refer anywhere.
NIL basically is an untypes REF (pointer) that does not refer anywhere.


Line 85: Line 79:
if x is null then
if x is null then
display dialog "x is null"
display dialog "x is null"
end if
end if</lang>
</lang>
=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>
<lang AutoHotkey>If (object == null)
If (object == null)
MsgBox, object is null</lang>
MsgBox, object is null
</lang>


=={{header|C}}==
=={{header|C}}==
Line 111: Line 102:
=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
As with Java, any reference type may be null, and testing for nullity uses ordinary boolean operators.
As with Java, any reference type may be null, and testing for nullity uses ordinary boolean operators.
<lang csharp>
<lang csharp>if (foo == null)
if (foo == null)
Console.WriteLine("foo is null");</lang>
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.
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 sharp|C#|2.0+}}
{{works with|C sharp|C#|2.0+}}
<lang csharp>
<lang csharp>int? x = 12;
int? x = 12;
x = null;</lang>
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:
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 sharp|C#|2.0+}}
{{works with|C sharp|C#|2.0+}}
<lang csharp>
<lang csharp>Console.WriteLine(name ?? "Name not specified");
Console.WriteLine(name ?? "Name not specified");


//Without the null coalescing operator, this would instead be written as:
//Without the null coalescing operator, this would instead be written as:
Line 135: Line 121:
//}else{
//}else{
// Console.WriteLine(name);
// Console.WriteLine(name);
//}
//}</lang>
</lang>


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
Line 163: Line 148:
=={{header|E}}==
=={{header|E}}==


object == null
<lang e>object == null</lang>


=={{header|Forth}}==
=={{header|Forth}}==
Line 174: Line 159:
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.
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
<lang haskell>undefined -- undefined value provided by the standard library
error "oops" -- another undefined value
error "oops" -- another undefined value
head [] -- undefined, you can't take the head of an empty list</lang>
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:
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:
Line 186: Line 171:
There are many ways to work with Maybe, but here's a basic case expression:
There are many ways to work with Maybe, but here's a basic case expression:


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


=={{header|Io}}==
=={{header|Io}}==
if(object == nil, "object is nil" println)
<lang io>if(object == nil, "object is nil" println)</lang>


=={{header|J}}==
=={{header|J}}==
Line 210: Line 195:


=={{header|Logo}}==
=={{header|Logo}}==
to test :thing
<lang logo>to test :thing
if empty? :thing [print [list or word is empty]]
if empty? :thing [print [list or word is empty]]
end
end


print empty? [] ; true
print empty? [] ; true
print empty? "|| ; true
print empty? "|| ; true</lang>


=={{header|Mathematica}}==
=={{header|Mathematica}}==
Mathematica can assign a Null value to a symbol, two examples:
Mathematica can assign a Null value to a symbol, two examples:
<lang Mathematica>
<lang Mathematica>x=Null;</lang>
<lang Mathematica>x =.
x=Null;
x = (1 + 2;)
</lang>
FullForm[x]</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: ===):
Both set x to be Null. To specifically test is something is Null one can use the SameQ function (with infix operator: ===):
<lang Mathematica>
<lang Mathematica>SameQ[x,Null]</lang>
SameQ[x,Null]
</lang>
Or equivalent:
Or equivalent:
<lang Mathematica>
<lang Mathematica>x===Null</lang>
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.
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:
To test if an object has something assigned (number, list, graphics, null, infinity, symbol, equation, pattern, whatever) one uses ValueQ:
<lang Mathematica>
<lang Mathematica>x =.;
ValueQ[x]
x =.;
x = 3;
ValueQ[x]
ValueQ[x]</lang>
x = 3;
ValueQ[x]
</lang>
gives:
gives:
<lang Mathematica>
<lang Mathematica>False
True</lang>
False
True
</lang>


=={{header|MAXScript}}==
=={{header|MAXScript}}==
<pre>if obj == undefined then print "Obj is undefined"</pre>
<lang maxscript>if obj == undefined then print "Obj is undefined"</lang>


=={{header|Modula-3}}==
=={{header|Modula-3}}==
Line 280: Line 253:
=={{header|OCaml}}==
=={{header|OCaml}}==
Maybe the closest type of OCaml would be the type option, which is defined like this in the standard library:
Maybe the closest type of OCaml would be the type option, which is defined like this in the standard library:
<lang ocaml>
<lang ocaml>type 'a option = None | Some of 'a</lang>
<lang ocaml>match v with
type 'a option = None | Some of 'a
| None -> "unbound value"
</lang>
| Some _ -> "bounded value"</lang>
<lang ocaml>
match v with
| None -> "unbound value"
| Some _ -> "bounded value"
</lang>


=={{header|Perl}}==
=={{header|Perl}}==
Line 310: Line 279:
print "x is None"
print "x is None"
else:
else:
print "x is not None"
print "x is not None"</lang>
</lang>
Output:<pre>
Output:<pre>
x is None
x is None
Line 318: Line 286:
=={{header|R}}==
=={{header|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.
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>
<lang R>is.null(NULL) # TRUE
is.null(NULL) # TRUE
is.null(123) # FALSE
is.null(123) # FALSE
is.null(NA) # FALSE
123==NULL # Empty logical value, with a warning
is.null(NA) # FALSE
foo <- function(){} # function that does nothing
123==NULL # Empty logical value, with a warning
foo() # returns NULL</lang>
foo <- function(){} # function that does nothing
foo() # returns NULL
</lang>


=={{header|Ruby}}==
=={{header|Ruby}}==
Line 338: Line 304:
=={{header|Slate}}==
=={{header|Slate}}==
<lang slate>
<lang slate>Nil isNil = True.</lang>
Nil isNil = True.
</lang>




Line 351: Line 315:
=={{header|Standard ML}}==
=={{header|Standard ML}}==
Maybe the closest type of Standard ML would be the type option, which is defined like this in the standard library:
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>
datatype 'a option = NONE | SOME of 'a
<lang sml>case v of NONE => "unbound value"
| SOME _ => "bounded value"</lang>
</lang>
<lang sml>
case v of NONE => "unbound value"
| SOME _ => "bounded value"
</lang>


=={{header|Tcl}}==
=={{header|Tcl}}==