Login  Register

Re: difference between double dispatch and the method explains here

Posted by Roelof on Apr 08, 2019; 5:20am
URL: https://forum.world.st/difference-between-double-dispatch-and-the-method-explains-here-tp5097970p5098013.html

I can try to explain what I trying to solve.

I have a Robot which can turn left,  turn right or moveForward.

now I have a string like 'LAR'

that means the robot needs to turn left (l) , move forward one place (A) and turn left.
and I have to keep track to which direction the robot is facing and on which coordinate it stands.

so to summarize with the above string

lets say the robot is facing north on coordinate (0,0)
then it has to turn left , so its facing east and still on coordinate (0,0)
then it has to move forward, so its still  facing east but are on coordinate(0,1)
then it has to turn right, so its facing north and on coordinate (0,1)

and TimMacKinnon has challenged me to do this with double dispatch.

So I think now I need a object Direction, a sub object North and a sub - sub object TurnLeft, turnRight and moveForward.

So I can use double dispath first the direction North, East, South, West
and then use double dispatch to find the right move.

Roelof





Op 8-4-2019 om 06:50 schreef Richard O'Keefe:
It would really REALLY **REALLY** help if we knew what
the heck you were trying to do.  There is an excellent
chance that it is MUCH simpler than you think.  If you
cannot show us the Smalltalk version of the problem,
can you show us the version for some other language?


On Sun, 7 Apr 2019 at 20:15, Roelof Wobben <[hidden email]> wrote:
Op 6-4-2019 om 15:15 schreef K K Subbu:
> On 06/04/19 4:49 PM, Roelof Wobben wrote:
>> Hello,
>>
>> I just learned double dispatch.
>> And now for the Robot challenge of exercism Tim has pointed me to
>> this
>> article(https://blog.metaobject.com/2019/04/accessors-have-message-obsession.html)
>>
>> but I fail to see how the move method looks like in that article.
>> I had a conversation with Tim in the exercism channel and the way he
>> explains it, it looks like double dispatch for me.
>>
>> Am I on the right track or do I oversee something here.
> unary methods like moveRight perform specific ops and are not
> parametric, so only a single dispatch, depending on the receiver, is
> needed.
>
> If you change it to move: aDistanceOrAngle, then performing requests
> like "move: 3 cms" or "move: 30 degrees" will depend not only on the
> receiver but also on the class of the argument. This would need double
> dispatch (aka multiple polymorphism). The first dispatch would be
> based on the receiver and the receiver's method would then dispatch it
> based on the class of the argument (i.e. Distance>>move or Angle>>move )
>
> HTH .. Subbu
>
>


hmm, still stuck

I have now a class Direction with as instance variables north, south,
east, west
and made the accessors.

then I thought I need a initialize like this :

initialize
    north = Direction( 0, -1).
    east  = Direction( 1,  0).
    south = Direction( 0,  1).
    west  = Direction(-1,  0).

but the Direction (0,-1)  is a problem . the compiler does not like the
(0,-1) part

to give you the big picture. I have a Robot which can turnRight ,
turnLeft and moveForward and I try to understand how the page would work
in my case.

So I have a object Direction as described above and a Object MoveForward
which is a subobject of Direction.
MoveForward has only 1 method :

IsMove
    ^  'A'

Roelof