Multiple dispatch?

Q&A's, tips, howto's

Multiple dispatch?

Postby jopython » Mon Nov 10, 2014 2:30 am

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
If newLisp had a feature which allowed parametric polymorphism, my code will look more cleaner as it grows in size.
jopython
 
Posts: 123
Joined: Tue Sep 14, 2010 3:08 pm

Re: Multiple dispatch?

Postby ralph.ronnquist » Mon Nov 10, 2014 3:51 am

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.
ralph.ronnquist
 
Posts: 208
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

Re: Multiple dispatch?

Postby jopython » Thu Nov 13, 2014 11:10 pm

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"
jopython
 
Posts: 123
Joined: Tue Sep 14, 2010 3:08 pm

Re: Multiple dispatch?

Postby ryuo » Fri Nov 14, 2014 3:58 am

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.
ryuo
 
Posts: 43
Joined: Wed May 21, 2014 4:40 pm

Re: Multiple dispatch?

Postby ralph.ronnquist » Fri Nov 14, 2014 4:29 am

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.
ralph.ronnquist
 
Posts: 208
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia


Return to newLISP in the real world

Who is online

Users browsing this forum: No registered users and 1 guest

cron