Null object: Difference between revisions

m (→‎{{header|Sidef}}: minor code fix)
Line 1,025:
exceptions, at least until you try to use them as defined values,
in which case they're thrown for real.
 
=={{header|Phix}}==
Phix has a special unassigned value, that can be tested for using object(). There is no offical way to "un-assign" a variable, though you could fairly easily do so with a bit of inline assembly.
<lang Phix>object x
 
procedure test()
if object(x) then
puts(1,"x is an object\n")
else
puts(1,"x is unassigned\n")
end if
end procedure
 
test()
x = 1
test()</lang>
{{out}}
<pre>
x is unassigned
x is an object
</pre>
There is also a builtin NULL, however it is equivalent to the integer 0 and will trigger a type check if assigned to a variable declared as string or sequence. In most programs the zero-length string/sequence (""/{}) suffices,
but if you want a variable that can be a string/sequence or NULL, use something like the following user-defined types:
<lang Phix>type nullableString(object o)
return string(o) or o=NULL
end type
nullableString s
s = "hello"
s = NULL
--s = 1 -- error
--s = {1,2,3} -- error
 
type nullableSequence(object o)
return sequence(o) or o=NULL
end type
nullableSequence q
q = {1,2,3}
q = "string" -- fine (strings are a subset of sequences)
q = NULL
--q = 1 -- error</lang>
 
=={{header|PHL}}==
7,820

edits