Have you considered passing names of methods around?
class Foo {
method bar($a) { dd $a }
}
my $name = "bar";
Foo."$name"(42); # 42
The syntax calls for the need of stringifications and parentheses to indicate you want to call that method. If you really want to use the Method
object and pass that around, you can, but there is no real nice syntax for it:
class Foo {
method bar($a) { dd $a }
}
constant &B = Foo.^find_method("bar");
B(Foo, 42) # 42
In this example, the constant &B
is created at compile time. But you can also call this method at runtime, e.g. when the name of the method is in a variable:
my $name = "bar";
my $method = Foo.^find_method($name);
$method(Foo, 42);
However, in that case, you would probably be better of with the ."$name"()
syntax. See https://docs.raku.org/routine/find_method for a bit more info.
perl6 -e 'class Foo { method bar($a) { say $a } }; my &foosub = -> |c { Foo.^find_method("bar").(Foo, |c) }; &foosub(42)'
– Bordie