[Newbies] Polymorphic or: with block argument
johnson at cs.uiuc.edu
Sun Sep 10 13:50:02 UTC 2017
Yes, when the compiler sees exp or: [ ... ] then it assumes that "exp" is
a boolean-valued expression and generates code that fails if it isn't.
Not all Smalltalk compilers perform this optimization, but it is very
common. There are a bunch of methods treated like this, ifTrue:ifFalse: is
whileTrue: is an interesting slightly different case. The compiler
usually optimizes whileTrue: only when both receiver and the argument are
blocks. So, you can define whileTrue: in other classes without any
On Sun, Sep 10, 2017 at 6:33 AM, Lorenz Köhl <rainbowtwigs at gmail.com> wrote:
> [image: Boxbe] <https://www.boxbe.com/overview> This message is eligible
> for Automatic Cleanup! (rainbowtwigs at gmail.com) Add cleanup rule
> | More info
> I don't understand why Smalltalk doesn't allow me to have an or:
> method that takes a block argument (except on Boolean). For example:
> Set or: 1. ==> MNU as expected.
> Set or: . ==> NonBooleanReceiver exception.
> Set or: [:x | x] ==> Argument has too many arguments
> Is it because the compiler specializes this to boolean when it sees
> the or: [ ... ] syntax? Is this a tradeoff made for performance?
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Beginners