I have a script like the below. Intent is to have different filter methods to filter a list.
Here is the code.
2
3 class list_filter {
4 has @.my_list = (1..20);
5
6 method filter($l) { return True; }
7
8 # filter method
9 method filter_lt_10($l) {
10 if ($l > 10) { return False; }
11 return True;
12 }
13
14 # filter method
15 method filter_gt_10($l) {
16 if ($l < 10) { return False; }
17 return True;
18 }
19
20 # expecting a list of (1..10) to be the output here
21 method get_filtered_list_lt_10() {
22 return self.get_filtered_list(&{self.filter_lt_10});
23 }
24
25 # private
26 method get_filtered_list(&filter_method) {
27 my @newlist = ();
28 for @.my_list -> $l {
29 if (&filter_method($l)) { push(@newlist, $l); }
30 }
31 return @newlist;
32 }
33 }
34
35 my $listobj = list_filter.new();
36
37 my @outlist = $listobj.get_filtered_list_lt_10();
38 say @outlist;
Expecting [1..10] to be the output here. But getting following error.
Too few positionals passed; expected 2 arguments but got 1
in method filter_lt_10 at ./b.pl6 line 9
in method get_filtered_list_lt_10 at ./b.pl6 line 22
in block <unit> at ./b.pl6 line 37
What am I doing wrong here?
Found it. this is what worked for me.
Output:
TL;DR You told P6 what arguments to expect when calling your
filter
method. Then you failed to pass the agreed argument(s) when you called it. So P6 complained on your behalf. To resolve the issue, either pass the argument(s) you told P6 to expect or stop telling P6 to expect them. :)The message says expected
2
, got1
, rather than expected1
got0
.This is because
self
is implicitly passed and added to the "expected" and "got" totals in this appended bit of message detail, bumping both up by one. (This detail is perhaps Less Than Awesome, i.e. something we should perhaps consider fixing.)When I run your code on tio I get:
The method declaration
method filter($l) {...}
at line 27 tells P6 to expect two arguments for each.filter
method call:The invocant. (This will be bound to
self
.) Let's call that argument A.A positional argument. (This will be bound to the
$l
parameter). Let's call that argument B.But in
&{self.filter}
in line 12, while you provide the.filter
method call with an argument A, i.e. an invocant argument, you don't provide an argument B, i.e. a positional argument (afterfilter
, e.g.&{self.filter(42)}
).Hence
Too few positionals passed; expected 2 arguments but got 1
.Passing a method as a parameter in Perl 6 either requires you to use MOP (Meta-Object Protocol) methods, or pass the method by name (which would then do the lookup for you at runtime).
But why use
method
s if you're not really doing something with the object in those methods? They might as well besub
s then, which you can pass as a parameter.Perhaps this is best by example:
The first
sub filter
, which only returns a constant value (in this caseTrue
), can be represented much more easily in the signature with an empty body.The
filter_lt_10
andfilter_gt_10
subs only need the condition negated, hence the use of thenot
.The
get_filtered_list
method is supposed to be private, so make it a private method by prefixing!
.In the
get_filtered_list_lt_10
you now need to callget_filtered_list
with a!
instead of a.
. And you pass thefilter_lt_10
sub as a parameter by prefixing the&
(otherwise it would be considered a call to the sub without any parameters, which would fail).Change the
get_filtered_list
to use the built-ingrep
method: this takes aCallable
block that takes a single parameter and which should return somethingTrue
to include the value of the list it works upon. Since asub
taking a single parameter is aCallable
, we can just specify the sub there directly.Hope this made sense. I tried to stay as close as possible to the intended semantics.
Some general programming remarks: it feels to me that the naming of the subs is confusing: it feels to me that they should be called
filter_le_10
andfilter_ge_10
, because that's really what they do it appears to me. Also, if you really don't want any ad-hoc filtering, but only filtering from a specific set of predefined filters, you would probably be better of by creating a dispatch table using constants orenum
s, and use that to indicate which filter you want, rather than encoding this information in the name of yet another method to make and maintain.Hope this helps.
The
&{self.method}
syntax was new to me, so thanks for that. Unfortunately it doesn't work if parameters are needed. You can usesub
as other posters mentioned, but if you need to use methods, you can get a method by callingself.^lookup
, which is the use of the meta-object protocol that Elizabeth mentioned. ('^' means you're not calling a method that's part of that class, but rather part of the "shadow" class which contains the main class's guts / implementation details.)To get a method, use run
obj.^lookup(method name)
, and call it by passing in the object itself (often "self") as the first parameter, then the other parameters. To bind the object to the function so it doesn't need to be explicitly added each time, use the assuming function.