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) |
||
trace("object is null");</lang> |
|||
trace("object is null"); |
|||
⚫ | |||
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> |
||
⚫ | |||
=={{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: |
||
⚫ | |||
<pre> |
|||
⚫ | |||
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) |
||
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) |
||
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; |
||
x = null;</lang> |
|||
⚫ | |||
</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}}== |
||
<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> |
<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: |
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> |
<lang haskell>case thing of |
||
Nothing -> "It's Nothing. Or null, whatever." |
|||
Just v -> "It's not Nothing; it is " ++ show v ++ "."</lang> |
|||
=={{header|Io}}== |
=={{header|Io}}== |
||
<lang io>if(object == nil, "object is nil" println)</lang> |
|||
=={{header|J}}== |
=={{header|J}}== |
||
Line 210: | Line 195: | ||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
<lang logo>to test :thing |
|||
if empty? :thing [print [list or word is empty]] |
|||
end |
|||
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> |
||
⚫ | |||
x=Null; |
|||
⚫ | |||
</lang> |
|||
⚫ | |||
⚫ | |||
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 =.; |
||
⚫ | |||
x =.; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
x = 3; |
|||
ValueQ[x] |
|||
</lang> |
|||
gives: |
gives: |
||
<lang Mathematica> |
<lang Mathematica>False |
||
⚫ | |||
False |
|||
True |
|||
</lang> |
|||
=={{header|MAXScript}}== |
=={{header|MAXScript}}== |
||
< |
<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> |
||
⚫ | |||
type 'a option = None | Some of 'a |
|||
⚫ | |||
</lang> |
|||
⚫ | |||
⚫ | |||
match v with |
|||
⚫ | |||
⚫ | |||
</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(123) # FALSE |
|||
is.null(NA) # FALSE |
|||
⚫ | |||
is.null(NA) # FALSE |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
</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> |
|||
<lang sml>case v of NONE => "unbound value" |
|||
⚫ | |||
</lang> |
|||
<lang sml> |
|||
case v of NONE => "unbound value" |
|||
⚫ | |||
</lang> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |