Null object: Difference between revisions
Content added Content deleted
(→{{header|Vlang}}: Rename "Vlang" in "V (Vlang)") |
(→{{header|TXR}}: New section.) |
||
Line 2,101: | Line 2,101: | ||
if {![dict exists $dic nullval]} ...</syntaxhighlight> |
if {![dict exists $dic nullval]} ...</syntaxhighlight> |
||
Note that lists do not support anything like nulls, since they are strictly sequences of values. |
Note that lists do not support anything like nulls, since they are strictly sequences of values. |
||
=={{header|TXR}}== |
|||
=={{trans|Common Lisp}}== |
|||
TXR Lisp has a <code>nil</code> symbol which serves as the empty list and Boolean false, like Common Lisp and similar dialects. It is a very important symbol which plays a central role. |
|||
Variable definitions, global and local, without initial value expressions take on the value <code>nil</code>. Elements of newly created vectors and structure slots are <code>nil</code> by default. Optional function parameters that don't receive arguments are given <code>nil</code> arguments by default. Many functions which search for something use <code>nil</code> for indicating not found. |
|||
Object-oriented programming in TXR Lisp is done with structures, which do not provide a CLOS-like object system. The <code>nil</code> object isn't a structure, and so it cannot take slot references, or method invocation. Only structures have methods, which means that it's not possible to define a null object method <code>m</code> such that <code>obj.(m)</code> can be invoked if <code>obj</code> is an expression evaluating to <code>nil</code>. |
|||
The Gang-of-Five Null Object Pattern can be employed: defining struct types that behave like null. This null object doesn't have to be related by inheritance to the structs in conjunction with which it is used. |
|||
<syntaxhighlight lang="txrlisp"> |
|||
(defstruct null-widget () |
|||
(:method popularity (me) 0)) |
|||
(defvarl null-widget (new null-widget)) |
|||
(defstruct real-widget () |
|||
pop |
|||
(:method popularity (me) me.pop))</syntaxhighlight> |
|||
In situations when a null object would be used simply to provide safe treatment of null without the verbosity of checks for its presence, and a default value of <code>nil</code> is acceptable, TXR Lisp has null-safe slot and method access: <code>obj.?slot</code>, <code>obj.?(method arg)</code>. |
|||
The expression <code>w.?(popularity)</code> will evaluate to <code>nil</code> if <code>w</code> is <code>nil</code>, being equivalent to <code>(if w w.(popularity))</code>, except that <code>w</code> is evaluated only once. |
|||
Like in Common Lisp, <code>nil</code> an instance of the type <code>null</code>, which is a unit type having only that instance. And <code>nil</code> is also the name of the bottom type of the type system: the <code>nil</code> type is the subtype of every type, including itself. |
|||
Like in Common Lisp, the expression <code>(or a b c ...)</code> evaluates the arguments from left to right, returning the value of the leftmost one which does not evaluate to <code>nil</code>. |
|||
<syntaxhighlight lang="txrlisp">;; Find widget in one of three places in order |
|||
(defun find-widget (name) |
|||
(or [%widget-hash% name] |
|||
(lookup-local-widget name) |
|||
(lookup-global-widget name)))</syntaxhighlight> |
|||
=={{header|Ursa}}== |
=={{header|Ursa}}== |