【发布时间】:2018-07-22 09:05:55
【问题描述】:
我想获得一个对象方法的指针,例如这个类
class Foo {
has $thing = "baz";
method bar() { say $thing }
};
sub quux( Callable $flimflam ) {
$flimflam()
};
my $foo = Foo.new;
我想获取$foo.bar 方法指针以将其传递给quux。然而,这
quux(&($foo.bar))
Type check failed in binding to parameter '$flimflam'; expected Callable but got Bool (Bool::True) in sub quux 失败
这也不行
quux($foo.bar)
也许是因为它没有参数;但是,如果我们将bar 的定义更改为:
method bar($some ) { say $some ~ $thing }
然后上面调用的错误变成Too few positionals passed; expected 2 arguments but got 1 in method bar,错误爬到bar本身,这意味着对象本身没有进入。
我有checked out this answer,但它是用于类方法(新),它涉及使用元对象协议。会有更简单的方法吗?
【问题讨论】:
标签: functional-programming raku