double dispatch example

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

double dispatch example

stepharo
Hi

If you happen to know a double dispatch situation in Pharo, I'm
interested since I'm revisiting my lecture.


Stef


Reply | Threaded
Open this post in threaded view
|

Re: double dispatch example

abergel
Hi Stef!

Plenty of examples:

- Converting money
- Paper, Stone, Scissor
- A canvas containing triangle, circle, box has to be printed, on an html canvas or PDF. (Very close to the Visitor design pattern, but simpler)

Here are the lectures I use:
https://dl.dropboxusercontent.com/u/31543901/TMP/DoubleDispatch.zip

(One of them is highly inspired from a lecture from Oscar)

Hope it helps!

Cheers,
Alexandre

> On Sep 13, 2016, at 7:33 AM, stepharo <[hidden email]> wrote:
>
> Hi
>
> If you happen to know a double dispatch situation in Pharo, I'm interested since I'm revisiting my lecture.
>
>
> Stef
>
>


Reply | Threaded
Open this post in threaded view
|

Re: double dispatch example

stepharo
Thanks Alex

I was thinking about the code in Pharo.

Plenty of examples:
> - Converting money

can you tell me more about this one?

> - Paper, Stone, Scissor
> - A canvas containing triangle, circle, box has to be printed, on an html canvas or PDF. (Very close to the Visitor design pattern, but simpler)
>
> Here are the lectures I use:
> https://dl.dropboxusercontent.com/u/31543901/TMP/DoubleDispatch.zip

TX
here is what I wrote today



>
> (One of them is highly inspired from a lecture from Oscar)

I will have a look now

>
> Hope it helps!
>
> Cheers,
> Alexandre
>
>> On Sep 13, 2016, at 7:33 AM, stepharo <[hidden email]> wrote:
>>
>> Hi
>>
>> If you happen to know a double dispatch situation in Pharo, I'm interested since I'm revisiting my lecture.
>>
>>
>> Stef
>>
>>
>
>


Design-DoubleDispatch.pdf (515K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: double dispatch example

Denis Kudriashov

2016-09-13 20:56 GMT+02:00 stepharo <[hidden email]>:

I was thinking about the code in Pharo.

Plenty of examples:
- Converting money

can you tell me more about this one?

Probably most known and beautiful case is arithmetic operations. Look at #adaptToInteger:andSend: and friends.
Reply | Threaded
Open this post in threaded view
|

Re: double dispatch example

Melanie Tarr
In reply to this post by abergel
Can someone please tell me how to unsubscribe from this list,

Kind Regards
Melanie

On Tue, Sep 13, 2016 at 10:16 PM, Alexandre Bergel <[hidden email]> wrote:
Hi Stef!

Plenty of examples:

- Converting money
- Paper, Stone, Scissor
- A canvas containing triangle, circle, box has to be printed, on an html canvas or PDF. (Very close to the Visitor design pattern, but simpler)

Here are the lectures I use:
https://dl.dropboxusercontent.com/u/31543901/TMP/DoubleDispatch.zip

(One of them is highly inspired from a lecture from Oscar)

Hope it helps!

Cheers,
Alexandre

> On Sep 13, 2016, at 7:33 AM, stepharo <[hidden email]> wrote:
>
> Hi
>
> If you happen to know a double dispatch situation in Pharo, I'm interested since I'm revisiting my lecture.
>
>
> Stef
>
>



Reply | Threaded
Open this post in threaded view
|

Re: double dispatch example

Ben Coman
http://lists.pharo.org/mailman/listinfo/pharo-users_lists.pharo.org

On Thu, Sep 15, 2016 at 10:04 AM, Melanie Tarr <[hidden email]> wrote:

> Can someone please tell me how to unsubscribe from this list,
>
> Kind Regards
> Melanie
>
> On Tue, Sep 13, 2016 at 10:16 PM, Alexandre Bergel <[hidden email]>
> wrote:
>>
>> Hi Stef!
>>
>> Plenty of examples:
>>
>> - Converting money
>> - Paper, Stone, Scissor
>> - A canvas containing triangle, circle, box has to be printed, on an html
>> canvas or PDF. (Very close to the Visitor design pattern, but simpler)
>>
>> Here are the lectures I use:
>> https://dl.dropboxusercontent.com/u/31543901/TMP/DoubleDispatch.zip
>>
>> (One of them is highly inspired from a lecture from Oscar)
>>
>> Hope it helps!
>>
>> Cheers,
>> Alexandre
>>
>> > On Sep 13, 2016, at 7:33 AM, stepharo <[hidden email]> wrote:
>> >
>> > Hi
>> >
>> > If you happen to know a double dispatch situation in Pharo, I'm
>> > interested since I'm revisiting my lecture.
>> >
>> >
>> > Stef
>> >
>> >
>>
>>
>

Reply | Threaded
Open this post in threaded view
|

Re: double dispatch example

abergel
In reply to this post by stepharo
> I was thinking about the code in Pharo.
>
> Plenty of examples:
>> - Converting money
>
> can you tell me more about this one?

I mean summing and converting different money. Here is the whole idea:

1 EUR = 662 CLP (Chilean pesos)

You have a class Money to which you can sum other money.

Object subclass: #Money instVarNames: ‘value’
“I omit here the accessors of value. Note that generating the accessors of value produces a method value1, you have to rename it"

Money>>+ anotherMoney
        self subclassResponsibility

Money>>sumWithEUR: money
        self subclassResponsibility

Money>>sumWithCLP: money
        self subclassResponsibility

Money>>= anotherMoney
        ^ self class == anotherMoney class and: [ self value = anotherMoney value ]

Money >>printOn: str
        "Useful for debugging"
        super printOn: str.
        str nextPut: $<.
        str nextPutAll: self value asString.
        str nextPut: $>.



You have two subclasses:
Money subclass: #EUR
Money subclass: #CLP

EUR>>+ anotherMoney
        ^ anotherMoney sumWithEUR: self

EUR>>sumWithEUR: money
        ^ EUR new value: self value + money value

EUR>>sumWithCLP: money
        ^ CLP new value: (self value * 662) + money value


CLP>>+ anotherMoney
        ^ anotherMoney sumWithCLP: self

CLP>>sumWithEUR: money
        ^ EUR new value: (self value / 662) + money value

CLP>>sumWithCLP: money
        ^ CLP new value: self value + money value


Here is a test:
TestCase subclass: #MoneyTest

MoneyTest>>testSum
        | clp1 eur1 clp2 eur2 |
        clp1 := CLP new value: 3500.
        eur1 := EUR new value: 10.
        clp2 := CLP new value: 5000.
        eur2 := EUR new value: 20.

        self assert: clp1 + clp2 equals: (CLP new value: 8500).
        self assert: clp1 + eur1 equals: (CLP new value: 3500 + 6620).
       
        self assert: eur1 + eur2 equals: (EUR new value: 30).
        self assert: eur1 + clp2 equals: (EUR new value: 5000 / 662 + 10).


To be really complete, the example also needs to implement hash and the test should also test equality.
I hope it helps!

Cheers,
Alexandre

>
>> - Paper, Stone, Scissor
>> - A canvas containing triangle, circle, box has to be printed, on an html canvas or PDF. (Very close to the Visitor design pattern, but simpler)
>>
>> Here are the lectures I use:
>> https://dl.dropboxusercontent.com/u/31543901/TMP/DoubleDispatch.zip
>
> TX
> here is what I wrote today
>
>
>
>>
>> (One of them is highly inspired from a lecture from Oscar)
>
> I will have a look now
>>
>> Hope it helps!
>>
>> Cheers,
>> Alexandre
>>
>>> On Sep 13, 2016, at 7:33 AM, stepharo <[hidden email]> wrote:
>>>
>>> Hi
>>>
>>> If you happen to know a double dispatch situation in Pharo, I'm interested since I'm revisiting my lecture.
>>>
>>>
>>> Stef
>>>
>>>
>>
>>
>
> <Design-DoubleDispatch.pdf>


Reply | Threaded
Open this post in threaded view
|

Re: double dispatch example

Pharo Smalltalk Users mailing list
In reply to this post by stepharo
On 13 September 2016 at 11:33, stepharo <[hidden email]> wrote:
> Hi
>
> If you happen to know a double dispatch situation in Pharo, I'm interested
> since I'm revisiting my lecture.
>

There is a good example in
http://www.lulu.com/shop/andres-valloud/fundamentals-of-smalltalk-programming-technique-volume-1/paperback/product-5299835.html

He uses it in an implementation of Game Of Life.

>
> Stef
>
>

Reply | Threaded
Open this post in threaded view
|

Re: double dispatch example

tblanchard
In reply to this post by Denis Kudriashov

On Sep 13, 2016, at 12:35, Denis Kudriashov <[hidden email]> wrote:


2016-09-13 20:56 GMT+02:00 stepharo <[hidden email]>:

I was thinking about the code in Pharo.

Probably most known and beautiful case is arithmetic operations. Look at #adaptToInteger:andSend: and friends.

That one is my go-to example to explain it.
Reply | Threaded
Open this post in threaded view
|

Re: double dispatch example

stepharo

Tx todd

For me it was too complex.

I should check the book and andres and now I used the examples given my alex.



Le 10/10/16 à 09:24, Todd Blanchard a écrit :

On Sep 13, 2016, at 12:35, Denis Kudriashov <[hidden email]> wrote:


2016-09-13 20:56 GMT+02:00 stepharo <[hidden email]>:

I was thinking about the code in Pharo.

Probably most known and beautiful case is arithmetic operations. Look at #adaptToInteger:andSend: and friends.

That one is my go-to example to explain it.