21

I'd like to find out if there is a way to bind a method and/or a sub to another method/sub name in Raku. I've seen how you can bind a variable to a method/sub, but that's not quite what I'm looking for. I know how to do it in Perl 5:

sub sub1 {
  print "sub1!\n";
}

*sub2 = \&sub1;

sub1(); # sub1!
sub2(); # sub1!
TylerH
  • 20,799
  • 66
  • 75
  • 101
JustThisGuy
  • 1,109
  • 5
  • 10

2 Answers2

18

Actually, what you do with normal variables is pretty much exactly what you do with subs.

sub sub1 { say "sub1!" }

my &sub2 = &sub1;

sub1; # sub1!
sub2; # sub1!

You don't need to bind, actually, because subs are not containerized and &-sigiled variables don't have special handling for assignment like @ and %-sigiled variables. (If you do a .WHICH or .WHERE you can see that they point to the same place in memory).

user0721090601
  • 5,276
  • 24
  • 41
  • "subs are not containerized and `&`-sigiled variables don't have special handling for assignment ... If you do a `.WHICH` or `.WHERE` you can see that they point to the same place in memory". That surprised me. Thanks for clearing up my mental model, though I first had to eliminate several misinterpretations I had of what you had written. ( [This may help other readers](https://tio.run/##K0gtyjH7/7@4NEkhpTS3QEFDJV5ToVqhoLREQUkvzDHIthpE6qVnFpfUKoCZ@WlQsfw0qHC1XriHp7MHhOEa5FqrpFBrzQUyEogNgaYVJ1YqKIHYikAZPTWQRXB5IyR5I2R5jdxKBTWgoLEmXATMVbAFCxtiFzbCFPbLzIEJ/v8PAA) ) – raiph Dec 06 '20 at 11:18
  • Thanks! I did quite a bit of searching, but I wasn't able to find an example of this. – JustThisGuy Dec 06 '20 at 17:59
12

@user0721090601 already gave the answer for subs. To do the same for methods is slightly more involved. Fortunately, there is a module in the ecosystem that makes that easier for you: Method::Also. This allows you to say:

use Method::Also;
# ...
method foo() is also<bar bazzy> {

And then you can call the .bar and .bazzy methods as well, and get the same result as calling the .foo method.

Elizabeth Mattijsen
  • 25,654
  • 3
  • 75
  • 105
  • 1
    Oops, totally forgot about handling methods. And that's good because they function quite differently so it led to your answer. I ended up using a technique similar in concept to what you have in `Method::Also`, just a bit more naïvely (read: without touching the HOW) in my update to CLDR (which I *think* is finally going to make it in time for a weekly) – user0721090601 Dec 06 '20 at 16:55
  • 1
    Thanks! I'll definitely check out Method::Also and also look under the hood. – JustThisGuy Dec 06 '20 at 18:00
  • 1
    @JustThisGuy under the hood it's fairly complex if you're not up to speed on HOW classes (most people aren't, and most don't ever need to touch them). You might also look at using the special method FALLBACK to redirect method calls – user0721090601 Dec 06 '20 at 19:30
  • Most of the HOW class handling is really to touch some corner cases and add support for "is also" to methods in roles. The part that really matters, is the `.^add_method` call, which takes a name and a `Method` object. – Elizabeth Mattijsen Dec 06 '20 at 19:33
  • @user0721090601, I'll take a look at FALLBACK. – JustThisGuy Dec 06 '20 at 21:02
  • @ElizabethMattijsen, thanks for the pointer on `.^add_method`. – JustThisGuy Dec 06 '20 at 21:02