Multiple dispatch?

Started by jopython, November 09, 2014, 06:30:31 PM

Previous topic - Next topic

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_dispatch#Common_Lisp">//http://en.wikipedia.org/wiki/Multiple_dispatch#Common_Lisp

If newLisp had a feature which allowed parametric polymorphism, my code will look more cleaner as it grows in size.

rrq

#1
It might not satisfy a purist, but you may implement it via "dual" contexts and a faked FOOP object, as in the following example:
(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

#2
This doesn't work. I get the following instead

(collide-with "asteroid" "asteroid") =>
   ("POFF" spaceship-spaceship "asteroid" "asteroid")

When I was expecting "BANG"

ryuo

#3
Try this fixed up version:



(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.

rrq

#4
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.