Help - Glossary - L - Logic and Squeak
If you open a Transcript (Cmd-t) and then select all of the following magenta text and Cmd-d you see a few interesting differences between standard propositional logic and how Squeak handles the nil:
Transcript clear.
Transcript show:
'The handling of ifTrue: is not quite in Smalltalk what one would expect,
given standard propositional logic.'; cr; cr.
Transcript show:
'[ :y :z | y not or: z ] value: false value: true >> ';
show: ([ :y :z | y not or: z ] value: false value: true).
Transcript cr; show:
'[ :y :z | y ifTrue: z ] value: false value: true >> ';
show: ([ :y :z | y ifTrue: z ] value: false value: true).
Transcript cr; show:
'[ :y :z | y not or: z ] value: false value: false >> ';
show: ([ :y :z | y not or: z ] value: false value: false).
Transcript cr; show:
'[ :y :z | y ifTrue: z ] value: false value: false >> ';
show: ([ :y :z | y ifTrue: z ] value: false value: false).
""
Transcript cr; cr; show:
'[ :y :z | y not or: z ] value: true value: true >> ';
show: ([ :y :z | y not or: z ] value: true value: true).
Transcript cr; show:
'[ :y :z | y ifTrue: z ] value: true value: true >> ';
show: ([ :y :z | y ifTrue: z ] value: true value: true).
Transcript cr; show:
'[ :y :z | y not or: z ] value: true value: false >> ';
show: ([ :y :z | y not or: z ] value: true value: false).
Transcript cr; show:
'[ :y :z | y ifTrue: z ] value: true value: false >> ';
show: ([ :y :z | y ifTrue: z ] value: true value: false).
"Transcript clear."
Transcript cr; cr; show:' More on the handling of ifTrue and nil ' ; cr; cr.
Transcript show:
'[ :y :z | y not or: z ] value: false value: nil >> ';
show: ([ :y :z | y not or: z ] value: false value: nil).
Transcript cr; show:
'[ :y :z | y ifTrue: z ] value: false value: nil >> ';
show: ([ :y :z | y ifTrue: z ] value: false value: nil).
Transcript cr; show:
'[ :y :z | y not or: z ] value: nil value: false >> ';
show: '** stops the system **'.
Transcript cr; show:
'[ :y :z | y ifTrue: z ] value: nil value: false >> ';
show: '** stops the system **'.
""
Transcript cr; cr; show:
'[ :y :z | y not or: z ] value: true value: nil >> ';
show: ([ :y :z | y not or: z ] value: true value: nil).
Transcript cr; show:
'[ :y :z | y ifTrue: z ] value: true value: nil >> ';
show: ([ :y :z | y ifTrue: z ] value: true value: nil).
Transcript cr; show:
'[ :y :z | y not or: z ] value: nil value: true >> ';
show: '** stops the system **'.
Transcript cr; show:
'[ :y :z | y ifTrue: z ] value: nil value: true >> ';
show: '** stops the system **'.
""
Transcript cr; cr; show:
'So one reason your code may fail is that it is conditionalizing on nil'.
One can test this sort of behaviour
quicker with Cmd-p on lines like the following - and note that three
out of four will start the debugger for you:
false ifTrue: true.
nil ifFalse: true.
nil not or: true.
false not or: nil.
But the point of this note is to make clear
that the possibility that a value may be nil in
Squeak - variables when initialized and given no value are initialized by
Squeak to the object nil - can lead to unexpected
behavior of code.
Glossary - L