Skip to content

Instantly share code, notes, and snippets.

@Xliff
Last active November 3, 2018 22:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xliff/34b9d2e3e27c76696ec60e6791075634 to your computer and use it in GitHub Desktop.
Save Xliff/34b9d2e3e27c76696ec60e6791075634 to your computer and use it in GitHub Desktop.
Method::Also and multi method support...

Noticed that you can get conflicts with Method::Also and multi methods. Try this bit:

use Method::Also

class M {
  multi method m is also<M> {
    self.m(42)
  }
  multi method m(Int $m) is also<M> {
    say "M is $m";
  }
}

M.m.say

You'll find that you get the following from the rakudo compiler:

Package 'M' already has a method 'M' (did you mean to declare a multi-method?)
at /home/cbwood/Projects/p6-GtkPlus/scripts/test/test-multi-alias.pl6:9

If in this case, you want all candidates of 'm' to be 'M', there is a solution. Thanks to the fabulous lizmat (the author of the module in question) the best solution is to add a proto method and leave the candidates as they are:

class M {
  proto method m(|) is also<M> {*}
  
  multi method m {
    self.m(42)
  }
  
  multi method m(Int $m) {
    say "M is $m";
  }
}

You have to admit, that does look a lot nicer!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment