Raku中的方法/子绑定

我想知道是否有办法将方法和/或子绑定到 Raku 中的另一个方法/子名称。我已经看到如何将变量绑定到方法/子,但这并不是我想要的。我知道如何在 Perl 5 中做到这一点:

sub sub1 {
  print "sub1!n";
}

*sub2 = &sub1;

sub1(); # sub1!
sub2(); # sub1!

回答

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 已经为订阅者提供了答案。对方法做同样的事情稍微复杂一些。幸运的是,生态系统中有一个模块可以让您更轻松:Method::Also。这允许你说:

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

然后你也可以调用.bar.bazzy方法,并获得与调用.foo方法相同的结果。


以上是Raku中的方法/子绑定的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>