oke,

so I need a single Object here
that contains this in a initialize function :

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



oke, then time to figure out how to change my functions because I have the feeling that I still miss
some of the pieces: 

Robot >> move: aInstruction
	aInstruction = $R
		ifTrue: [ ^ self direction: (PositioningSystem new turnRight: direction) ].
	aInstruction = $L
		ifTrue: [ ^ self direction: (PositioningSystem new turnLeft: direction) ].
	^ self
		position:
			(PositioningSystem new moveForWard: position direction: self direction)



PositionSystem class >>  initialize
    Directions := {('north' -> (0 @ 1)).
    ('east' -> (1 @ 0)).
    ('south' -> (0 @ -1)).
    ('west' -> (-1 @ 0))}

PositionSystem >> turnRight: aDirection
    "comment stating purpose of message"

    | old |
    old := Directions detect: [ :b | b key = aDirection ].
    ^ (Directions after: old ifAbsent: [ Directions first ]) key

Roelof





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



Reply via email to