没有这样的方法&lt;name&gt;用于 <class> 类型的调用者 [英] No such method &lt;name&gt; for invocant of type &lt;class&gt;

查看:31
本文介绍了没有这样的方法&lt;name&gt;用于 <class> 类型的调用者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含用于函数重载的 multi 定义的类,但是当我尝试调用该类和重载方法时,它会引发错误.可以运行以产生此错误的工作示例如下所示:

I've created a class which contains multi definitions for function overloading, however when I try to call the class and the overloaded method, it throws an error. A working example which can be run to produce this error is shown below:

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

我可能做错了,有人可以指出正确的方法吗?

I may be doing this wrong, can someone point me to the correct way to do this?

我正在有效地尝试将其变成一个模块,我可以将其用于其他用途.

I'm effectively trying to make this a module, which I can use for other things.

推荐答案

您需要在 test method 之前添加 self 关键字:

You need add the self keyword before your test method:

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 类中,使用 method 关键字来声明方法.

note: In Perl 6 class, use method keyword to declare a method.

这篇关于没有这样的方法&lt;name&gt;用于 <class> 类型的调用者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆