No such method <name> for invocant of type <class>

  • A+
Category:Languages

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); 

Error:

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?

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

 


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); 

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

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: