I am looking at the documentation on Bags https://docs.perl6.org/type/Bag
When I try
say kxxv($breakfast)
I get the error
===SORRY!=== Error while compiling:
Undeclared routine:
kxxv used at line 1
This also happens with say total($breakfast)
I am expecting both of those methods to work functionally as well as in an object style as most of the other methods do.
It only works if done using object notation
say $breakfast.kxxv
my $breakfast = bag <spam eggs spam spam bacon>
say kxxv($breakfast)
perl6 -v): Only some of the common methods are available as functions, not all of them.
You can use the alternate method calling form if you really want to place the method name first:
say kxxv $breakfast: (note the semicolon at the end). It basically follows the same format as you'd see in a signature for that method: :(Invocant: Arg1, Arg2) => method-name $invocant: $arg1, $arg2
Alternate solution: create your own kxxv subroutine (possibly with automatic coercion to Bag:
sub kxxv(Bag() $bag) { $bag.kxxv }
which would allow:
say kxxv <spam eggs spam spam bacon>;
If you really like to make it part of "your" language, you could put the sub into a module as:
sub kxxv(Bag() $bag) is export { $bag.kxxv }
and optionally put this in the ecosystem.
Only some of the common methods are available as functions, not all of them.
The other force at work here is whether the operation is very specific to that data type, or could apply to a wider range of them. For example, kv works reasonably across hashes, arrays, sets, bags, and so forth. By contrast, kxxv doesn't.
Ah, ok. Thanks.
Most helpful comment
The other force at work here is whether the operation is very specific to that data type, or could apply to a wider range of them. For example,
kvworks reasonably across hashes, arrays, sets, bags, and so forth. By contrast,kxxvdoesn't.