Page 1 of 1

Multiple dispatch?

Posted: Mon Nov 10, 2014 2:30 am
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.

Re: Multiple dispatch?

Posted: Mon Nov 10, 2014 3:51 am
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.

Re: Multiple dispatch?

Posted: Thu Nov 13, 2014 11:10 pm
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"

Re: Multiple dispatch?

Posted: Fri Nov 14, 2014 3:58 am
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.

Re: Multiple dispatch?

Posted: Fri Nov 14, 2014 4:29 am
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.