Multiple dispatch?

Q&A's, tips, howto's
Post Reply
jopython
Posts: 123
Joined: Tue Sep 14, 2010 3:08 pm

Multiple dispatch?

Post by jopython »

This was probably discussed in this forum before. Has anyone succeeded adding multiple dispatch capability to newLisp using a macro or otherwise?
http://en.wikipedia.org/wiki/Multiple_d ... ommon_Lisp
If newLisp had a feature which allowed parametric polymorphism, my code will look more cleaner as it grows in size.

ralph.ronnquist
Posts: 216
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

Re: Multiple dispatch?

Post by ralph.ronnquist »

It might not satisfy a purist, but you may implement it via "dual" contexts and a faked FOOP object, as in the following example:

Code: Select all

(define (dual a b) (list (context (sym (string a "-" b) MAIN))))

(define (collide-with a b) (:collide-with (dual (a 0) (b 0)) a b))

(context 'MAIN:asteroid-asteroid)
(define (collide-with a b)  (list "BANG" (context) a b))

(context 'MAIN:asteroid-spaceship)
(define (collide-with a b) (list "BONG" (context) a b))

(context 'MAIN:spaceship-asteroid)
(define (collide-with a b) (list "ZING" (context) a b))

(context 'MAIN:spaceship-spaceship)
(define (collide-with a b) (list "POFF" (context) a b))
Here the dual function simply creates the appropriate FOOP composition context to allow its "singular" polymorphism to apply.

jopython
Posts: 123
Joined: Tue Sep 14, 2010 3:08 pm

Re: Multiple dispatch?

Post by jopython »

This doesn't work. I get the following instead

Code: Select all

(collide-with "asteroid" "asteroid") =>
   ("POFF" spaceship-spaceship "asteroid" "asteroid")
When I was expecting "BANG"

ryuo
Posts: 43
Joined: Wed May 21, 2014 4:40 pm

Re: Multiple dispatch?

Post by ryuo »

Try this fixed up version:

Code: Select all

(context 'MAIN:asteroid-asteroid)
(define (collide-with a b)  (list "BANG" (context) a b))

(context 'MAIN:asteroid-spaceship)
(define (collide-with a b) (list "BONG" (context) a b))

(context 'MAIN:spaceship-asteroid)
(define (collide-with a b) (list "ZING" (context) a b))

(context 'MAIN:spaceship-spaceship)
(define (collide-with a b) (list "POFF" (context) a b))

(context 'MAIN)

(define (dual a b) (list (context (sym (string a "-" b) MAIN))))

(define (collide-with a b) (:collide-with (dual a b) a b))

(println (collide-with "asteroid" "asteroid"))

(exit)
Part of the problem with the original was it expected the arguments to be strings embedded in a list.

ralph.ronnquist
Posts: 216
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

Re: Multiple dispatch?

Post by ralph.ronnquist »

Well, yes, I had assumed one would be dealing with "typed" arguments, such as, say, (android 544) and (spaceship 345) etc, and not just argument types. In any case the point is to map the apparent plurality into a singular form, and then use the FOOP polymorphic dispatch.

Post Reply