Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Compiler error with ifTrue:ifFalse: ?

0 views
Skip to first unread message

Tim M

unread,
May 21, 2008, 3:31:18 AM5/21/08
to
I wanted to port some code from Squeak to Dolphin and noticed there are a
bunch of ifEmpty methods in Squeak that aren't in the Dolphin image.

I started creating a portability application and noticed that I get a compiler
error with the following method where it complains about emptyBlock:

ifNotEmptyDo: notEmptyBlock ifEmpty: emptyBlock
"Evaluate emptyBlock if I'm empty, notEmptyBlock otherwise
Evaluate the notEmptyBlock with the receiver as its argument"

^ self isEmpty ifFalse: [notEmptyBlock value: self] ifTrue: emptyBlock

however its sister method is fine?

ifEmpty: emptyBlock ifNotEmptyDo: notEmptyBlock
"Evaluate emptyBlock if I'm empty, notEmptyBlock otherwise"
"Evaluate the notEmptyBlock with the receiver as its argument"

^ self isEmpty ifTrue: emptyBlock ifFalse: [notEmptyBlock value: self]

Obviously I can work around it but it seems odd.

Tim


Dmitry Zamotkin

unread,
May 21, 2008, 4:22:04 AM5/21/08
to
#ifTrue:ifFalse: and #ifFalse:ifTrue: methods wait for block with
brackets for optimization.
Use next code:

self isEmpty ifFalse: [notEmptyBlock value: self] ifTrue: [ emptyBlock

value ]

Dmitry Zamotkin

Tim M

unread,
May 22, 2008, 4:51:44 AM5/22/08
to
Hi Dmitry,

> #ifTrue:ifFalse: and #ifFalse:ifTrue: methods wait for block with
> brackets for optimization.

Yes you normally get the warning in the transcript but it usually works.
However in the case of ifFalse:ifTrue: it actually gives an error and refuses
to compile. This seems wrong especially when ifTrue:ifFalse: doesn't?

I have applied the change that you suggest - but it does seem like an inconsistency.

Tim


Dmitry Zamotkin

unread,
May 22, 2008, 9:16:33 AM5/22/08
to
If you want to implement these methods in Collection I suppose they
should look like:

ifEmpty: emptyBlock ifNotEmptyDo: notEmptyBlock
^self isEmpty ifTrue: [emptyBlock value] ifFalse: [self do:
notEmptyBlock]

ifNotEmptyDo: notEmptyBlock ifEmpty: emptyBlock
^self ifEmpty: emptyBlock ifNotEmptyDo: notEmptyBlock

0 new messages