1

Am having a filter method in my baseclass in my perl 6 code. I am overriding the filter in derived classes. Something like following.

  3 class list_filter {
  4   has @.my_list = (1..20);
  5 
  6   # will be overriding this in derived classes
  7   method filter($l) { return True; }
  8 
  9   # same print method I will be calling from all derived class objects
 10   method print_filtered_list() {
 11     my @outlist = self.get_filtered_list(&{self.filter});
 12     say @outlist;
 13   }
 14 
 15   # private
 16   method get_filtered_list(&filter_method) {
 17     my @newlist = ();
 18     for @.my_list -> $l {
 19       if (&filter_method($l)) { push(@newlist, $l); }
 20     }
 21     return @newlist;
 22   }
 23 }
 24 
 25 class list_filter_lt_10 is list_filter {
 26   method filter($l) {
 27     if ($l > 10) { return False; }
 28     return True;
 29   }
 30 }
 31 
 32 class list_filter_gt_10 is list_filter {
 33   method filter($l) {
 34     if ($l < 10) { return False; }
 35     return True;
 36   }
 37 }
 38 
 39 my $listobj1 = list_filter_lt_10.new();
 40 $listobj1.print_filtered_list(); # expecting 1..10 here
 41 
 42 my $listobj2 = list_filter_gt_10.new();
 43 $listobj2.print_filtered_list(); # expecting 11..20 here

Want to have different derived class objects that just override the filter method and use the same base class functionality to print out the filtered list.

But getting an error like the following.

Too many positionals passed; expected 0 or 1 arguments but got 2
  in method print_filtered_list at ./b.pl6 line 11
  in block <unit> at ./b.pl6 line 40

How do I resolve this? I want to pass on a filter method as an argument to another method.

BhaskarS
  • 305
  • 1
  • 6

0 Answers0