Rakudo: Bag methods 'kxxv' and 'total' only work as object methods

Created on 9 May 2018  路  4Comments  路  Source: rakudo/rakudo

The Problem

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)

Expected Behavior

I am expecting both of those methods to work functionally as well as in an object style as most of the other methods do.

Actual Behavior

It only works if done using object notation
say $breakfast.kxxv

Steps to Reproduce

my $breakfast = bag <spam eggs spam spam bacon>
say kxxv($breakfast)

Environment

  • Operating system: Ubuntu 16.04.4 LTS
  • Rakudo Star version 2018.04.1 built on MoarVM version 2018.04.1 implementing Perl 6.c.Compiler version (perl6 -v):

Most helpful comment

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.

All 4 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gfldex picture gfldex  路  5Comments

zoffixznet picture zoffixznet  路  4Comments

eiro picture eiro  路  3Comments

Alihusejn picture Alihusejn  路  3Comments

jonathanstowe picture jonathanstowe  路  3Comments