Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you fill a Callable attribute via clone?

Tags:

raku

class Foo {
    has &.bar;
    has @.quux is required;
    method clone { nextwith :quux(@!quux.clone) };
    # as per <https://docs.perl6.org/type/Mu#method_clone>
};
my $f = Foo.new(quux => []);
my $f-clone = $f.clone(bar => sub { die });
# Foo.new(bar => Callable, quux => [])

but should be

Foo.new(bar => sub { #`(Sub|94789546929784) ... }, quux => [])

Adding :bar(&!bar.clone) to the nextwith call does not help.

like image 841
daxim Avatar asked Feb 11 '19 14:02

daxim


1 Answers

nextwith "calls the next matching candidate with arguments provided by users". You're only passing a :quux argument in the nextwith call.

Unless you add an explicit slurpy hash parameter (eg. *%foo), all methods have an implicit *%_ in their signature:

say .signature given method ($a, $b) {} # (Mu: $a, $b, *%_)

So by default all named arguments are slurped into %_. A common idiom is to pass these on:

method clone { nextwith :quux(@!quux.clone), |%_ }

The above will pass arguments provided to the $f.clone call onto the nextwith'd clone call.

Adding :bar(&!bar.clone) to the nextwith call does not help.

That will be instead of the :bar argument passed in the $f.clone call. The &!bar in the original object contains the Callable type object.

like image 155
raiph Avatar answered Sep 30 '22 01:09

raiph