[Newbies] Polymorphic or: with block argument

Marcel Taeumel marcel.taeumel at hpi.de
Sun Sep 10 12:50:08 UTC 2017


Hi, there.

I think the issue is that you cannot implement the message #or: for any domain object because the compiler treats it in a special way.

Consider a class Foo with the method #or:

Foo >> #or: block
   ^ block value

You cannot use it as expected:

x := Foo new.
y := x or: [42].

There is an optimisation that interferes with the general concepts of objects and messaging.

Best,
Marcel
Am 10.09.2017 14:45:31 schrieb David T. Lewis <lewis at mail.msen.com>:
On Sun, Sep 10, 2017 at 01:33:15PM +0200, Lorenz K??hl wrote:
> 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?


Hi Lorenz,

If you are looking for set operations, check these methods:

Collection>>union:
Collection>>intersection:
Collection>>difference:

I am not sure what you are trying to do with your examples. Can you
explain the intent?

Dave

_______________________________________________
Beginners mailing list
Beginners at lists.squeakfoundation.org
http://lists.squeakfoundation.org/mailman/listinfo/beginners
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.squeakfoundation.org/pipermail/beginners/attachments/20170910/eccaf7f9/attachment.html>


More information about the Beginners mailing list