2 ms·
You can do multiple dispatching in Python, by hand or with decorators, but it can get kind of awkward, not as elegant as CLOS's and Dylan's approach with generi
by DonHopkins 8y ago
You can do multiple dispatching in Python, by hand or with decorators, but it can get kind of awkward, not as elegant as CLOS's and Dylan's approach with generic functions.
https://www.artima.com/weblogs/viewpost.jsp?thread=101605 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/latest/MultipleDispatching.html https://python-3-patterns-idioms-test.readthedocs.io/en/late...
https://pythonhosted.org/multidispatch/ https://pythonhosted.org/multidispatch/
https://adambard.com/blog/implementing-multimethods-in-python/ https://adambard.com/blog/implementing-multimethods-in-pytho...
https://opendylan.org/documentation/intro-dylan/multiple-dispatch.html https://opendylan.org/documentation/intro-dylan/multiple-dis...
https://opendylan.org/documentation/intro-dylan/objects.html#generic-functions-objects https://opendylan.org/documentation/intro-dylan/objects.html...
https://en.wikipedia.org/wiki/Multiple_dispatch https://en.wikipedia.org/wiki/Multiple_dispatch
https://en.wikipedia.org/wiki/Generic_function https://en.wikipedia.org/wiki/Generic_function
- andreareina 8y agoHah, I should have read further downthread to this before telling you about multiple dispatch via decorators.
- b2gills 8y agoIn 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.)