News for hackers. Stuff that matters.
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.)

There are no comments on this comment yet.