<class> 类型的调用者没有这样的方法 <name>
No such method <name> for invocant of type <class>
我创建了一个 class,其中包含 multi
函数重载定义,但是当我尝试调用 class 和重载方法时,它会抛出错误。下面显示了一个可以 运行 产生此错误的工作示例:
class Test
{
multi test(@data) {
return test(@data, @data.elems);
}
multi test(@data, $length) {
return 0;
}
}
my @t = 't1', 't2', 't3';
say Test.test(@t);
错误:
No such method 'test' for invocant of type 'Test'. Did you mean any of these?
List
Set
gist
list
in block <unit> at test.p6 line 13
我可能做错了,有人能告诉我正确的方法吗?
编辑:我正在有效地尝试使它成为一个模块,我可以将其用于其他事情。
您需要在 test
方法之前添加 self
关键字:
class Test
{
multi method test(@data) {
return self.test(@data, @data.elems);
}
multi method test(@data, $length) {
return 0;
}
}
my @t = 't1', 't2', 't3';
say Test.test(@t);
注意: 在 Perl 6 class 中,使用 method
关键字声明方法。
您收到 no such method
错误的原因是 multi
默认为 sub
除非另有说明。你需要 multi method test
其他答案必须有助于解释 multi method
的用法,但可选参数可能是获得相同结果的更简单方法:
#!/usr/bin/env perl6
use v6;
class Test
{
method test(@data, Int $length = @data.elems) {
say 'In method test length ', $length, ' data ', @data.perl;
return 0;
}
}
my @t = 't1', 't2', 't3';
say Test.test(@t);
我创建了一个 class,其中包含 multi
函数重载定义,但是当我尝试调用 class 和重载方法时,它会抛出错误。下面显示了一个可以 运行 产生此错误的工作示例:
class Test
{
multi test(@data) {
return test(@data, @data.elems);
}
multi test(@data, $length) {
return 0;
}
}
my @t = 't1', 't2', 't3';
say Test.test(@t);
错误:
No such method 'test' for invocant of type 'Test'. Did you mean any of these?
List
Set
gist
list
in block <unit> at test.p6 line 13
我可能做错了,有人能告诉我正确的方法吗?
编辑:我正在有效地尝试使它成为一个模块,我可以将其用于其他事情。
您需要在 test
方法之前添加 self
关键字:
class Test
{
multi method test(@data) {
return self.test(@data, @data.elems);
}
multi method test(@data, $length) {
return 0;
}
}
my @t = 't1', 't2', 't3';
say Test.test(@t);
注意: 在 Perl 6 class 中,使用 method
关键字声明方法。
您收到 no such method
错误的原因是 multi
默认为 sub
除非另有说明。你需要 multi method test
其他答案必须有助于解释 multi method
的用法,但可选参数可能是获得相同结果的更简单方法:
#!/usr/bin/env perl6
use v6;
class Test
{
method test(@data, Int $length = @data.elems) {
say 'In method test length ', $length, ' data ', @data.perl;
return 0;
}
}
my @t = 't1', 't2', 't3';
say Test.test(@t);