Re:A Conversation with Language Creators: Guido, James, Ande...
by b2gills on Thursday April 11, 2019 @10:43PM
(#19641480)
In Perl6 multiple dispatch is a fundamental feature. Even the infix numeric addition operator is handled by 27 multi subs. (They usually get inlined or even optimised out entirely.)
say &infix:«+».candidates.elems; # 27
Here is a quick translation of that second link to Perl6 #! /usr/bin/env perl6
use v6.d;
enum Roshambo < Rock Paper Scissors >;
enum Outcome < Lose Win Draw >;
proto compete ( Roshambo, Roshambo --> Outcome ){*}
multi compete ( Paper, Rock --> Win ){}
multi compete ( Rock, Scissors --> Win ){}
multi compete ( Scissors, Paper --> Win ){}
multi compete ( $a, $b where $b eqv $a --> Draw ){}
multi compete ( $, $ --> Lose ){}
for Roshambo.roll(20) Z Roshambo.roll(20) -> ($a,$b) {
say "$a.fmt('%8s'), $b.fmt('%8s'): &compete($a,$b)"
}
(Note that I didn't really write any actual code for the `compete` subroutines; it was all handled by the signatures.)
Re:A Conversation with Language Creators: Guido, James, Ande...
by andreareina on Thursday April 04, 2019 @10:25AM
(#19572641)
Hah, I should have read further downthread to this before telling you about multiple dispatch via decorators.
https://www.artima.com/weblogs/viewpost.jsp?thread=101605
Five-minute Multimethods in Python, by Guido van van Rossum.
I used to believe that multimethods were so advanced I would never need them. Well, maybe I still believe that, but here's a quick and dirty implementation of multimethods so you can see for yourself. Some assembly required; advanced functionality left as an exercise for the reader.
https://python-3-patterns-idioms-test.readthedocs.io/en/late...
https://pythonhosted.org/multidispatch/
https://adambard.com/blog/implementing-multimethods-in-pytho...
https://opendylan.org/documentation/intro-dylan/multiple-dis...
https://opendylan.org/documentation/intro-dylan/objects.html...
https://en.wikipedia.org/wiki/Multiple_dispatch
https://en.wikipedia.org/wiki/Generic_function