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 another.

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 problem.

-Ralph Johnson

On Sun, Sep 10, 2017 at 6:33 AM, Lorenz Köhl <rainbowtwigs@gmail.com> wrote:
Boxbe This message is eligible for Automatic Cleanup! (rainbowtwigs@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: [1]. ==> 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?

greetings,
  Lorenz

_______________________________________________
Beginners mailing list
Beginners@lists.squeakfoundation.org
http://lists.squeakfoundation.org/mailman/listinfo/beginners