Undefined values/Check if a variable is defined
From Rosetta Code
You are encouraged to solve this task according to the task description, using any language you may know.
Show how to access null in your language by checking to see if an object is equivalent to the null object.
Note: the title of this article does not appropriately represent the task. The task is about "null"-like values in various languages, which may or may not be related to the defined-ness of variables in your language.
[edit] ActionScript
if (object == null)
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.
var foo; // untyped
var bar:*; // explicitly untyped
trace(foo + ", " + bar); // outputs "undefined, undefined"
if (foo == undefined)
trace("foo is undefined"); // outputs "foo is undefined"
[edit] Ada
with Ada.Text_Io;
if Object = null then
Ada.Text_Io.Put_line("object is null");
end if;
[edit] ALGOL 68
In ALGOL 68 the NIL yields a name that does not refer to any value. NIL can never be naturally coerced and can only appear where the context is strong.
Works with: ALGOL 68 version Revision 1 - no extensions to language used
Works with: ALGOL 68G version Any - tested with release 1.18.0-9h.tiny
Works with: ELLA ALGOL 68 version Any (with appropriate job cards) - tested with release 1.8-8d
REF STRING no result = NIL;
STRING result := "";
IF no result :=: NIL THEN print(("no result :=: NIL", new line)) FI;
IF result :/=: NIL THEN print(("result :/=: NIL", new line)) FI;
IF no result IS NIL THEN print(("no result IS NIL", new line)) FI;
IF result ISNT NIL THEN print(("result ISNT NIL", new line)) FI;
COMMENT using the UNESCO/IFIP/WG2.1 ALGOL 68 character set
result := °;
IF REF STRING(result) :?: ° THEN print(("result ? °", new line)) FI;
END COMMENT
# Note the following gotcha: #
REF STRING var := NIL;
IF var ISNT NIL THEN print(("The address of var ISNT NIL",new line)) FI;
IF var IS REF STRING(NIL) THEN print(("The address of var IS REF STRING(NIL)",new line)) FI
Output:
no result :=: NIL result :/=: NIL no result IS NIL result ISNT NIL The address of var ISNT NIL The address of var IS REF STRING(NIL)
NIL basically is an untyped ref (pointer) that does not refer anywhere.
ALGOL 68 also has empty. This is a "constant" of size 0 and type void. c.f. Roots of a function for two different examples of usage.
- empty as an undefined argument to a routine.
- empty as a routine return if no result is found.
empty is typically used to refer to am empty leaf in a tree structure.
Basically:
- ALGOL 68's empty is python's
None, - ALGOL 68's void is python's
NoneType, and - ALGOL 68's nil is python's
hash(None)
[edit] AmigaE
DEF x : PTR TO object
-> ...
IF object <> NIL
-> ...
ENDIF
[edit] AppleScript
Many applications will return missing value, but null is also available.
if x is missing value then
display dialog "x is missing value"
end if
if x is null then
display dialog "x is null"
end if
[edit] AutoHotkey
If (object == null)
MsgBox, object is null
[edit] C
C's access to null is by way of a macro which simply evaluates to (void*) 0.
#include <stdio.h>
#include <stdlib.h>
if (object == NULL) {
printf("object is null");
}
[edit] C++
In C++ non-pointer types do not support null. (C++ provides value semantics rather than reference semantics). When using pointers C++ permits checking for null by comparing the pointer to a literal of 0, or (as in C) by way of a macro (NULL) which simply expands to 0.
#include <iostream>
#include <cstdlib>
if (object == 0) {
std::cout << "object is null";
}
boost::optional is available for cases where the programmer wishes to pass by value, but still support a null value.
#include <boost/optional.hpp>
#include <iostream>
boost::optional<int> maybeInt()
int main()
{
boost::optional<int> maybe = maybeInt();
if(!maybe)
std::cout << "object is null\n";
}
[edit] C#
As with Java, any reference type may be null, and testing for nullity uses ordinary boolean operators.
if (foo == null)
Console.WriteLine("foo is null");
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# version 2.0+
int? x = 12;
x = 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# version 2.0+
Console.WriteLine(name ?? "Name not specified");
//Without the null coalescing operator, this would instead be written as:
//if(name == null){
// Console.WriteLine("Name not specified");
//}else{
// Console.WriteLine(name);
//}
[edit] Clojure
Clojure's nil is equivalent to Java's null.
(let [x nil]
(println "Object is" (if (nil? x) "nil" "not nil")))
Test wether symbol foo is defined:
(find (ns-interns *ns*) 'foo)
Undefining foo:
(ns-unmap *ns* 'foo)
[edit] Common Lisp
nil is in fact the only false value. So a regular Boolean test will suffice to determine whether an object is nil.
(format t "The object is~A nil"
(if some-object " not" ""))
There is also the function null which tests whether some value is nil; it is equivalent to not but for the intent conveyed.
On the other hand, a symbol bound to nil is distinct from a symbol without any bindings. Trying to read the value of unbound symbol is an error of type unbound-variable. You can test whether a symbol is globally bound with boundp.
(format t "The symbol is~A bound"
(if (boundp 'some-object) "" " not"))
[edit] D
Library: tango In D is is used for comparing identity, like two objects or an object against null.
import tango.io.Stdout;
if(object is null)
{
Stdout.formatln("object is null");
}
[edit] E
object == null
[edit] Factor
: is-f? ( obj -- ? ) f = ;
[edit] Forth
Standard ANS Forth does not distinguish a particular invalid memory value like NULL. Instead, ALLOCATE returns an out-of-band success code to indicate a failed allocation. Dictionary words have the option of throwing an exception on a dictionary space overrun. Forth lacks a NULL symbol because it has such a wide variety of target platforms. On some embedded targets, the memory space may be as small as 64 direct-mapped addresses, where eliminating a valid zero address would have a high price.
In practice, all notable hosted implementations follow the C practice of being able to treat a zero address (i.e. FALSE) as a null address for the purpose of list termination.
[edit] 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 ++ "."
[edit] Icon and Unicon
Icon/Unicon have a null value/datatype. It isn't possible to undefine a variable.
[edit] Icon
procedure main()
nulltest("a",a) # unassigned variables are null by default
nulltest("b",b := &null) # explicit assignment is possible
nulltest("c",c := "anything")
nulltest("c",c := &null) # varibables can't be undefined
end
procedure nulltest(name,var)
return write(name, if /var then " is" else " is not"," null.")
end
[edit] Unicon
This Icon solution works in Unicon.
[edit] Io
if(object == nil, "object is nil" println)
[edit] J
J doesn't have NULL. To indicate "missing data", "normal" data is usually pressed into service (e.g. 0 or _1 in a numeric context, ' ' in a literal context, a: in a boxed context, etc). Frequently, missing data is represented by the empty vector '', or other arrays without any elements.
However, undefined names in J can be identified:
isUndefined=: _1 = nc@boxxopen
Example use:
isUndefined 'foo'
1
foo=:9
isUndefined 'foo'
0
Note, however, that this "name is not defined" state is not a first class value in J -- you can not create a list of "undefineds".
Note: the concept of an empty array can be natural in J (and APL) for representing data which is not there -- it is the structural equivalent of the number zero. That said, its implications can sometimes be non-obvious for people coming from a languages which requires that arrays have content. As a result, you will sometimes encounter empty array jokes...
- Marie Pennysworth, having spent a productive day shopping, stopped by Robert Cuttingham's butcher shop.
- "How much for your t-bones," she asked.
- "Eleven dollars per pound," he responded.
- "How about for your sirloin?" she continued.
- "Sirloin is thirteen dollars per pound today," he answered.
- "But Harkin's Grocery down the street is selling sirloin for nine dollars per pound!" she exclaimed.
- "So, why don't you buy it from them?" he asked.
- "Well, they're out," she sighed.
- He smiled, "When I am out, I only charge seven dollars a pound."
[edit] Java
In Java, "null" is a value of every reference type.
// here "object" is a reference
if (object == null) {
System.out.println("object is null");
}
[edit] JavaScript
if (object == null)
alert("object is null")
[edit] Logo
to test :thing
if empty? :thing [print [list or word is empty]]
end
print empty? [] ; true
print empty? "|| ; true
[edit] Lua
isnil = (object == nil)
print(isnil)
[edit] Mathematica
Mathematica can assign a Null value to a symbol, two examples:
x=Null;
x =.
x = (1 + 2;)
FullForm[x]
Both set x to be Null. To specifically test is something is Null one can use the SameQ function (with infix operator: ===):
SameQ[x,Null]
Or equivalent:
x===Null
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:
x =.;
ValueQ[x]
x = 3;
ValueQ[x]
gives:
False
True
[edit] MAXScript
if obj == undefined then print "Obj is undefined"
[edit] Modula-3
In Modula-3, NIL is a value, and NULL is a type. The NULL type contains only one value, NIL. NULL is a subtype of all reference types, which allows all reference types to have the value NIL.
This can lead to errors, if for example you write:
VAR foo := NIL
This (most likely incorrectly) gives foo the type NULL, which can only have the value NIL, so trying to assign it anything else will not work. To overcome this problem, you must specify the reference type when declaring foo:
VAR foo: REF INTEGER := NIL;
IF foo = NIL THEN
IO.Put("Object is nil.\n");
END;
[edit] MUMPS
A variable can be declared implicitly by using it as on the left side in a SET, or by making a new version for the current scope with a NEW statement. A variable can have descendants without having a value set.
The $DATA (or $D) function will return a number:
| $DATA returns: | Variable is defined | ||
|---|---|---|---|
| Variable has children | No | Yes | |
| No | 0 | 1 | |
| Yes | 10 | 11 | |
Or, by examples (in immediate mode):
CACHE>WRITE $DATA(VARI)
0
CACHE>SET VARI="HELLO" WRITE $DATA(VARI)
1
CACHE>NEW VARI WRITE $DATA(VARI) ;Change to a new scope
0
CACHE 1S1>SET VARI(1,2)="DOWN" WRITE $DATA(VARI)
10
CACHE 1S1>WRITE $DATA(VARI(1))
10
CACHE 1S1>WRITE $D(VARI(1,2))
1
CACHE 1S1>SET VARI(1)="UP" WRITE $DATA(VARI(1))
11
<CACHE 1S1>QUIT ;Leave the scope
<CACHE>W $DATA(VARI)," ",VARI
1 HELLO
[edit] Objective-C
The value nil is used to indicate that an object pointer (variable of type id) doesn't point to a valid object.
// here "object" is an object pointer
if (object == nil) {
NSLog("object is nil");
}
An interesting thing is that in Objective-C, it is possible to send a message to nil, and the program will not crash or raise an exception (nothing will be executed and nil will be returned in place of the usual return value).
[nil fooBar];
Note that nil is distinct from NULL, which is only used for regular C pointers.
For class pointers (values of type Class), they have a separate null pointer value called Nil.
Confusingly, there is also NSNull, a singleton class with one value, [NSNull null], used as a dummy object to represent the lack of a useful object. This is needed in collections like arrays and dictionaries, etc., because they do not allow nil elements, so if you want to represent some "empty" slots in the array you would use this.
[edit] 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"
[edit] Oz
There is no explicit null in Oz.
[edit] Unbound variables
If an unbound variable is accessed, the current thread will be suspended:
declare
X
in
{Show X+2} %% blocks
If you later assign a value to X in another thread, the original thread will resume and print the result of the addition. This is the basic building block of Oz' declarative concurrency.
[edit] Undefined values
Access to undefined values (like using an out-of-range array index or a non-existing record feature) will usually provoke an exception in Oz.
It is also possible to assign a unique "failed" value to a variable. Such a failed value encapsulates an exception. This can be useful in concurrent programming to propagate exceptions across thread boundaries.
declare
X = {Value.failed dontTouchMe}
in
{Wait X} %% throws dontTouchMe
Sometimes algebraic data types like Haskell's Maybe are simulated using records.
declare
X = just("Data")
in
case X of nothing then skip
[] just(Result) then {Show Result}
end
[edit] Perl
In Perl, undef is a special scalar value, kind of like null in other languages. A scalar variable that has been declared but has not been assigned a value will be initialized to undef. (Array and hash variables are initialized to empty arrays or hashes.)
If strict mode is not on, you may start using a variable without declaring it; it will "spring" into existence, with value undef. In strict mode, you must declare a variable before using it. Indexing an array or hash with an index or key that does not exist, will return undef (however, this is not an indication that the index or key does not exist; rather, it could be that it does exist, and the value is undef itself). If warnings is on, most of the time, if you use the undef value in a calculation, it will produce a warning. undef is considered false in boolean contexts.
It is possible to use undef like most other scalar values: you can assign it to a variable (either by doing $var = undef; or undef($var);), return it from a function, assign it to an array element, assign it to a hash element, etc. When you do list assignment (i.e. assign a list to a list of variables on the left side), you can use undef to "skip" over some elements of the list that you don't want to keep.
You can check to see if a value is undef by using the defined operator:
print defined($x) ? 'Defined' : 'Undefined', ".\n";
From the above discussion, it should be clear that if defined returns false, it does not mean that the variable has not been set; rather, it could be that it was explicitly set to undef.
Starting in Perl 5.10, there is also a defined-or operator in Perl. For example:
say $number // "unknown";
prints $number if it is defined (even if it is false) or the string "unknown" otherwise.
[edit] PHP
There is a special value NULL. You can test for it using is_null() or !isset()
$x = NULL;
if (is_null($x))
echo "\$x is null\n";
[edit] PicoLisp
New internal symbols are initialized with the value NIL. NIL is also the value for "false", so there is never really an "undefined value". 'not' is the predicate to check for NIL, but many other (typically flow control) functions can be used.
(if (not MyNewVariable)
(handle value-is-NIL) )
or
(unless MyNewVariable
(handle value-is-NIL) )
[edit] PowerShell
In PowerShell the automatic variable $null represents a null value.
if ($object -eq $null) {
...
}
[edit] PureBasic
All variables that has not yet been given any other value will be initiated to #Null
If variable = #Null
Debug "Variable has no value"
EndIf
[edit] Python
x = NoneOutput:
if x is None:
print "x is None"
else:
print "x is not None"
x is None
[edit] 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.
is.null(NULL) # TRUE
is.null(123) # FALSE
is.null(NA) # FALSE
123==NULL # Empty logical value, with a warning
foo <- function(){} # function that does nothing
foo() # returns NULL
[edit] REBOL
x: none
print ["x" either none? x ["is"]["isn't"] "none."]
Output:
x is none.
[edit] Ruby
if object.nil?
puts "object is null"
end
[edit] Scheme
(null? object)
Note: "null?" here tests whether a value is the empty list.
[edit] Slate
Nil isNil = True.
[edit] Smalltalk
object isNil ifTrue: [ "true block" ]
ifFalse: [ "false block" ].
nil isNil ifTrue: [ 'true!' displayNl ]. "output: true!"
[edit] Standard ML
Maybe the closest type of Standard ML would be the type option, which is defined like this in the standard library:
datatype 'a option = NONE | SOME of 'a
case v of NONE => "unbound value"
| SOME _ => "bounded value"
[edit] Tcl
In Tcl, where every value is a string, there is no out-of band value corresponding to NULL. In many cases, using the empty string is sufficient:
if {$value eq ""} ...
A stricter approximation to NULL can be had with non-existing variables or elements of a dict or array:
if ![info exist nullvar] ...
if ![info exists arr(nullval)] ...
if ![dict exists $dic nullval] ...
You can also use this condition inside an if statement to check if the variable $value is set or not:
[expr {([string compare "$value[puts \"\"]" ""]==1)}]
I think this is the better way to achieve it. In this case, if $value is set, returns true, or else returns false.

