Null object: Difference between revisions

Line 895:
<lang smalltalk>object isNil ifTrue: [ "true block" ]
ifFalse: [ "false block" ].
nil isNil ifTrue: [ 'true!' displayNl ]. "output: true!"</lang>
foo isNil ifTrue: [ 'ouch' displayNl ].
x := (foo == nil).
x := foo isNil</lang>
 
notice that nil is the singleton instance of the UndefinedObject class; i.e. it is a first class object. Thus we can do:
<lang smalltalk>foo := nil.
foo class. "-> UndefinedObject"
foo respondsTo: #'bar'. "asking if a message is implemented"
 
foo class compile:'fancyOperation ^ 123'.
foo fancyOperation "->123"</lang>
 
the last example being for demonstration only - it is not considered well behaved to add arbitrary code that way, except for framework support, such as encoding, decoding marshalling etc.)
 
=={{header|Standard ML}}==
Anonymous user