【发布时间】:2017-06-17 04:01:37
【问题描述】:
在 Perl 6(一种多分派语言)中,您可以找出是否存在与名称匹配的方法。如果有,您将获得与该名称匹配的 Method 对象列表:
class ParentClass {
multi method foo (Str $s) { ... }
}
class ChildClass is ParentClass {
multi method foo (Int $n) { ... }
multi method foo (Rat $r) { ... }
}
my $object = ChildClass.new;
for $object.can( 'foo' )
.flatmap( *.candidates )
.unique -> $candidate {
put join "\t",
$candidate.package.^name,
$candidate.name,
$candidate.signature.perl;
};
ParentClass foo :(ParentClass $: Str $s, *%_)
ChildClass foo :(ChildClass $: Int $n, *%_)
ChildClass foo :(ChildClass $: Rat $r, *%_)
这很好,但工作量很大。我宁愿有一些更简单的东西,例如:
$object.can( 'foo', $signature );
我或许可以做很多工作来实现这一点,但我是否错过了一些已经存在的东西?
【问题讨论】: