Null object: Difference between revisions
Content added Content deleted
m (fix link) |
m (→{{header|Phix}}: added syntax colouring the hard way) |
||
Line 1,549: | Line 1,549: | ||
There is 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, |
There is 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, but not other arbitrary integer/float values, use something like the following user-defined types: |
but if you want a variable that can be a string/sequence or NULL, but not other arbitrary integer/float values, 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,2,3} -- error |
|||
<!--<lang Phix>--> |
|||
type nullableSequence(object o) |
|||
<span style="color: #008080;">type</span> <span style="color: #000000;">nullableString</span><span style="color: #0000FF;">(</span><span style="color: #004080;">object</span> <span style="color: #000000;">o</span><span style="color: #0000FF;">)</span> |
|||
return sequence(o) or o=NULL |
|||
<span style="color: #008080;">return</span> <span style="color: #004080;">string</span><span style="color: #0000FF;">(</span><span style="color: #000000;">o</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">or</span> <span style="color: #000000;">o</span><span style="color: #0000FF;">=</span><span style="color: #004600;">NULL</span> |
|||
end type |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">type</span> |
|||
nullableSequence q |
|||
<span style="color: #000000;">nullableString</span> <span style="color: #000000;">s</span> |
|||
q = {1,2,3} |
|||
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"hello"</span> |
|||
q = "string" -- fine (strings are a subset of sequences) |
|||
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">NULL</span> |
|||
q = NULL |
|||
<span style="color: #000080;font-style:italic;">--s = 1 -- error |
|||
--q = 1 -- error</lang> |
|||
⚫ | |||
<span style="color: #008080;">type</span> <span style="color: #000000;">nullableSequence</span><span style="color: #0000FF;">(</span><span style="color: #004080;">object</span> <span style="color: #000000;">o</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #008080;">return</span> <span style="color: #004080;">sequence</span><span style="color: #0000FF;">(</span><span style="color: #000000;">o</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">or</span> <span style="color: #000000;">o</span><span style="color: #0000FF;">=</span><span style="color: #004600;">NULL</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">type</span> |
|||
<span style="color: #000000;">nullableSequence</span> <span style="color: #000000;">q</span> |
|||
<span style="color: #000000;">q</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">}</span> |
|||
<span style="color: #000000;">q</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"string"</span> <span style="color: #000080;font-style:italic;">-- fine (strings are a subset of sequences)</span> |
|||
<span style="color: #000000;">q</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">NULL</span> |
|||
<span style="color: #000080;font-style:italic;">--q = 1 -- error</span> |
|||
<!--</lang>--> |
|||
See also [[Undefined_values#Phix|Undefined_values]] |
See also [[Undefined_values#Phix|Undefined_values]] |
||