3 ms·
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 opt
by b2gills 8y ago
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.)