genuine squeak newbie
Bert Freudenberg
bert at isgnw.CS.Uni-Magdeburg.De
Sun Jun 20 21:18:39 UTC 1999
On Sun, 20 Jun 1999, montgomery f. tidwell wrote:
> [...]
>
> i don't understand the difference between ifTrue:/and: and
> ifFalse:/or:. it seems to me that they accomplish the same thing.
#and:/#or: always answer Booleans, #ifTrue:/#ifFalse: answer the value of
the block or nil. That means only #ifTrue:/#ifFalse: can be used for
returning conditional evaluation like C's "c = cond ? a : b" construct:
c := condition ifTrue: [a] ifFalse: [b].
#and:/#or: are used instead of #&/#| for partial evaluation of conditions,
like
(obj notNil and: [obj needsSomething]) ifTrue: [obj giveSomething]
Partial evaluation is almost always a Good Thing, that's why you rarely
see the other operators in Smalltalk code.
But there's something else: What is common in shell scripts or Perl like
test -r file && echo File Found
would be considered bad style in Smalltalk. Nobody would write
file isReadable and: [Transcript show: 'Found'].
but use the form
file isReadable ifTrue: [Transcript show: 'Found'].
although both variants do exactly the same. But the second is much
clearer, isn't it?
/bert
--
Bert Freudenberg Department of
Simulation and
Computer Graphics
http://isgwww.cs.uni-magdeburg.de/isg/bert.html Univ. of Magdeburg
More information about the Squeak-dev
mailing list
|