Subscripts

Accessing data structure elements by index or key

Often one needs to refer to a specific element (or specific slice of elements) of a collection or data structure. Borrowing from mathematical notation where the components of a vector v would be referred to as v₁, v₂, v₃, this concept is called "subscripting" (or "indexing") in Perl 6.

Basics

Perl 6 provides two universal subscripting interfaces:

elements are identified by interface name supported by
[ ] zero-based indices Positional Array, List, Buf, Match, ...
{ } string or object keys Associative Hash, Bag, Mix, Match, ...

Subscripts can be applied to any expression that returns a subscriptable object, not just to variables:

say "__Hello__".match(/__(.*)__/)[0];   # OUTPUT: «「Hello」␤» 
say "__Hello__".match(/__(.*)__/).[0];  # same, in method notation 

Positional and associative subscripting are not mutually exclusive - for example, Match objects support both (each accessing a different set of data). Also, to make list processing more convenient, class Any provides a fallback implementation for positional subscripts which simply treats the invocant as a list of one element. (But there's no such fallback for associative subscripts, so they throw a run-time error when applied to an object that does not implement support for them.)

say 42[0];    # OUTPUT: «42␤» 
say 42<foo>;  # ERROR: Type Int does not support associative indexing. 

Nonexistent elements

What happens when a nonexistent element is addressed by a subscript, is up to the collection type in question. Standard Array and Hash collections return the type object of their value type constraint (which, by default, is Any) unless the collection has been declared with the is default trait in which case the returned value will be whatever the programmer declared it to be:

# no default values specified 
my @array1;     say @array1[10];                   # OUTPUT: «(Any)␤» 
my Int @array2say @array2[10];                   # OUTPUT: «(Int)␤» 
 
my %hash1;      say %hash1<foo>;                   # OUTPUT: «(Any)␤» 
my Int %hash2;  say %hash2<foo>;                   # OUTPUT: «(Int)␤» 
 
# default values specified 
my @array3 is default('--');     say @array3[10];  # OUTPUT: «--␤» 
my Int @array4 is default(17);   say @array4[10];  # OUTPUT: «17␤» 
 
my %hash3 is default('Empty');   say %hash3<foo>;  # OUTPUT: «Empty␤» 
my Int %hash4 is default(4711);  say %hash4<foo>;  # OUTPUT: «4711␤» 

However, other types of collections may react differently to subscripts that address nonexistent elements:

say (01020)[3];           # OUTPUT: «Nil␤» 
say bag(<a a b b b>)<c>;      # OUTPUT: «0␤» 
say array[uint8].new(12)[2# OUTPUT: «0␤» 

To silently skip nonexistent elements in a subscripting operation, see Truncating slices and the :v adverb.

From the end

Positional indices are counted from the start of the collection, but there's also a notation for addressing elements by their position relative to the end: *-1 refers to the last element, *-2 to the second-to-last element, and so on.

my @alphabet = 'A' .. 'Z';
say @alphabet[*-1];  # OUTPUT: «Z␤» 
say @alphabet[*-2];  # OUTPUT: «Y␤» 
say @alphabet[*-3];  # OUTPUT: «X␤» 

Note: The asterisk is important. Passing a bare negative integer (e.g. @alphabet[-1]) like you would do in many other programming languages, throws an error in Perl 6.

What actually happens here, is that an expression like *-1 declares a code object via Whatever-currying - and the [ ] subscript reacts to being given a code object as an index, by calling it with the length of the collection as argument and using the result value as the actual index. In other words, @alphabet[*-1] becomes @alphabet[@alphabet.elems - 1].

This means that you can use arbitrary expressions which depend on the size of the collection:

say @array[* div 2];  # select the middlemost element 
say @array[$i % *];   # wrap around a given index ("modular arithmetic") 
say @array-> $size { $i % $size } ];  # same as previous 

Slices

When multiple elements of a collection need to be accessed, there's a shortcut to doing multiple separate subscripting operations: Simply specify a list of indices/keys in the subscript, to get back a list of elements - also called a "slice" - in the same order.

For positional slices, you can mix normal indices with from-the-end ones:

my @alphabet = 'a' .. 'z';
dd @alphabet[154*-911];  # OUTPUT: «("p", "e", "r", "l")␤» 

For associative slices, the angle-brackets form often comes in handy:

my %color = kiwi => "green"banana => "yellow"cherry => "red";
dd %color{"cherry""kiwi"};  # OUTPUT: «("red", "green")␤» 
dd %color<cherry kiwi>;       # OUTPUT: «("red", "green")␤» 
dd %color{*};                 # OUTPUT: «("green", "red", "yellow")␤» 

Be aware that slices are controlled by the type of what is passed to (one dimension of) the subscript, not its length:

subscript result
any Iterable not mentioned below normal slice
a Range L<truncates|#Truncating slices> in [ ]
a lazy Iterable L<truncates|#Truncating slices> in [ ]
* (Whatever-star) full slice (as if all keys/indices were specified)
any other object single-element access rather than a slice
Callable Whatever is returned by the callable. This can lead to recursion.
empty L<Zen slice|#Zen slices>

So even a one-element list returns a slice, whereas a bare scalar value doesn't:

dd @alphabet[2,];  # OUTPUT: «("c",)␤» 
dd @alphabet[2];   # OUTPUT: «"c"␤» 

(The angle-bracket form for associative subscripts works out because word quoting conveniently returns a Str in case of a single word, but a List in case of multiple words.)

In fact, the list structure of (the current dimension of) the subscript is preserved across the slice operation (but the kind of Iterable is not – the result is always just lists.)

dd @alphabet[0, (1..2, (3,))];  # OUTPUT: «("a", (("b", "c"), ("d",)))␤» 
dd @alphabet[0, (1..2, [3,])];  # OUTPUT: «("a", (("b", "c"), ("d",)))␤» 
dd @alphabet[flat 0, (1..2, (3,))];  # OUTPUT: «("a", "b", "c", "d")␤» 
dd flat @alphabet[0, (1..2, (3,))];  # OUTPUT: «("a", "b", "c", "d")␤» 

Truncating slices

Normally, referring to nonexistent elements in a slice subscript causes the output list to contain undefined values (or whatever else the collection in question chooses to return for nonexistent elements). However if the outer object passed to (one dimension of) of a positional subscript is a Range, it will be automatically truncated to the actual size of the collection:

my @letters = <a b c d e f>;
dd @letters[34567];  # OUTPUT: «("d", "e", "f", Any, Any)␤» 
dd @letters[3 .. 7];         # OUTPUT: «("d", "e", "f")␤» 

From-the-end indices are allowed as range end-points.

say @array[*-3 .. *];       # select the last three elements 

A similar thing is done for lazy sequences, but it is often impossible to determine whether the sequence is infinite. Just as often, the first part of the sequence is already known, and it would be silly to pretend we did not know it. As a stopgap measure to prevent runaway generation of huge lists, a lazy subscript will not truncate as long as it does not have to lazily generate values, but once it starts generating values lazily, it will stop if it generates a value that points to a nonexistent index.

dd @letters[024 ... *];     # Every other element of the array. 

This feature is more for protection against accidental out-of-memory problems than for actual use. Since some lazy sequences cache their results, every time they are used in a truncation, they accumulate one more known element. Things like this should probably be avoided rather than used for effect:

my @a = 23 ... *;
dd flat @letters[07@a]; # OUTPUT: «("a", Any, "c", "d", "e", "f")␤» 
dd flat @letters[07@a]; # OUTPUT: «("a", Any, "c", "d", "e", "f", Any)␤» 

The runaway protection is not perfect. The indices are eagerly evaluated, with the only stop condition being truncation. This is to provide mostly consistent results when there is self-reference/mutation inside the indices. As such, the following will most likely hang until all memory has been consumed:

@letters[0 xx *];

So, to safely use lazy indices, they should be one-shot things which are guaranteed to overrun the array. The following alternate formulation will produce a fully lazy result (but will not truncate):

my $a = (0 xx *).map({ @letters[$_}); # "a", "a", "a" ... forever 

If you don't want to specify your slice as a range/sequence but still want to silently skip nonexistent elements, you can use the :v adverb.

Zen slices

If you write a subscript without specifying any indices/keys at all, it simply returns the subscripted object itself. Since it is empty but returns everything, it is known as a "Zen slice".

It is different both from passing a Whatever-star (which, like a normal slice, always returns a List of elements no matter the type of the original object) and from passing an empty list (which returns an empty slice):

my %bag := ("orange" => 1"apple" => 3).Bag;
dd %bag<>;    # OUTPUT: «("orange"=>1,"apple"=>3).Bag␤» 
dd %bag{};    # OUTPUT: «("orange"=>1,"apple"=>3).Bag␤» 
dd %bag{*};   # OUTPUT: «(1, 3)␤» 
dd %bag{()};  # OUTPUT: «()␤» 

It is usually used to interpolate entire arrays / hashes into strings:

my @words = "cruel""world";
say "Hello, @words[]!"  # OUTPUT: «Hello, cruel world!␤» 

Multiple dimensions

Dimensions in subscripts are separated by a semicolon, allowing to mix lists of elements and dimensions.

my @twodim = (<a b c>, (123));
dd @twodim;
# OUTPUT: «Array @twodim = [("a", "b", "c"), (1, 2, 3)]␤» 
dd @twodim[0,1;1]; # 2nd element of both lists 
# OUTPUT: «("b", 2)␤» 

Multidimensional subscripts can be used to flatten nested lists.

my @toomany = [[<a b>], [12]];
dd @toomany;
# OUTPUT: «Array @toomany = [["a", "b"], [1, 2]]␤» 
dd @toomany[*;*];
# OUTPUT: «("a", "b", 1, 2)␤» 

You can use Whatever to select ranges or "rows" in multidimensional subscripts.

my @a = [[1,2], [3,4]];
say @a[*;1]; # 2nd element of each sub list 
# OUTPUT: «(2 4)␤» 
my @a = (<1 c 6>, <2 a 4>, <5 b 3>);
say @a.sort(*[1]); # sort by 2nd column 
# OUTPUT: «((2 a 4) (5 b 3) (1 c 6))␤» 

Modifying elements

Autovivification

Subscripts participate in "autovivification", i.e. the process by which arrays and hashes automatically spring into existence when needed, so that you can build nested data structures without having to pre-declare the collection type at each level:

my $beatles;
 
$beatles{"White Album"}[0= "Back in the U.S.S.R.";  # autovivification! 
 
say $beatles.perl;  # OUTPUT: «${"White Album" => $["Back in the U.S.S.R."]}␤» 

$beatles started out undefined, but became a Hash object because it was subscripted with { } in the assignment. Similarly, $beatles{"White Album"} became an Array object due to being subscripted with [ ] in the assignment.

Note that the subscripting itself does not cause autovivification: It only happens when the result of the subscripting chain is assigned to (or otherwise mutated).

Binding

A subscripting expression may also be used as the left-hand-side of a binding statement. If supported by the subscripted collection's type, this replaces whatever value container would be naturally found at that "slot" of the collection, with the specified container.

The built-in Array and Hash types support this in order to allow building complex linked data structures:

my @a = 10111213;
my $x = 1;
 
@a[2:= $x;  # binding! (@a[2] and $x refer to the same container now.) 
 
$x++@a[2]++;
 
dd @a;  # OUTPUT: «Array @a = [10, 11, 3, 13]␤» 
dd $x;  # OUTPUT: «Int $x = 3␤» 

See method BIND-POS and method BIND-KEY for the underlying mechanism.

Adverbs

The return value and possible side-effect of a subscripting operation can be controlled using adverbs.

Beware of the relatively loose precedence of operator adverbs, which may require you to add parentheses in compound expressions:

    if $foo || %hash<key>:exists { ... }    # WRONG, tries to adverb the || op 
    if $foo || (%hash<key>:exists{ ... }  # correct 
    if $foo or %hash<key>:exists { ... }    # also correct 

The supported adverbs are:

:exists

Return whether or not the requested element exists, instead of returning the element's actual value. This can be used to distinguish between elements with an undefined value, and elements that aren't part of the collection at all:

my @foo = Any10;
dd @foo[0].defined;    # OUTPUT: «False␤» 
dd @foo[0]:exists;     # OUTPUT: «True␤» 
dd @foo[2]:exists;     # OUTPUT: «False␤» 
dd @foo[02]:exists;  # OUTPUT: «(True, False)␤» 
 
my %fruit = apple => Anyorange => 10;
dd %fruit<apple>.defined;       # OUTPUT: «False␤» 
dd %fruit<apple>:exists;        # OUTPUT: «True␤» 
dd %fruit<banana>:exists;       # OUTPUT: «False␤» 
dd %fruit<apple banana>:exists# OUTPUT: «(True, False)␤» 

May also be negated to test for non-existence:

dd %fruit<apple banana>:!exists# OUTPUT: «(False, True)␤» 

To check if all elements of a slice exist, use an all junction:

if all %fruit<apple orange banana>:exists { ... }

:exists can be combined with the :delete and :p/:kv adverbs - in which case the behavior is determined by those adverbs, except that any returned element value is replaced with the corresponding Bool indicating element existence.

See method EXISTS-POS and method EXISTS-KEY for the underlying mechanism.

:delete

Delete the element from the collection or, if supported by the collection, creates a hole at the given index, in addition to returning its value.

my @tens = 0102030;
dd @tens[3]:delete;     # OUTPUT: «Int @tens = 30␤» 
dd @tens;               # OUTPUT: «Array @tens = [0, 10, 20]␤» 
 
my %fruit = apple => 5orange => 10banana => 4peach => 17;
dd %fruit<apple>:delete;         # OUTPUT: «5␤» 
dd %fruit<peach orange>:delete;  # OUTPUT: «(17, 10)␤» 
dd %fruit;                       # OUTPUT: «Hash %fruit = {:banana(4)}␤» 

Note that assigning Nil will revert the container at the given index to its default value. It will not create a hole. The created holes can be tested for with :exists but iteration will not skip them and produce undefined values instead.

my @a = 123;
@a[1]:delete;
say @a[1]:exists;
# OUTPUT: «False␤» 
.say for @a;
# OUTPUT: «1␤(Any)␤3␤» 

With the negated form of the adverb, the element is not actually deleted. This means you can pass a flag to make it conditional:

dd %fruit<apple> :delete($flag);  # deletes the element only if $flag is 
                                  # true, but always returns the value. 

Can be combined with the :exists and :p/:kv/:k/:v adverbs - in which case the return value will be determined by those adverbs, but the element will at the same time also be deleted.

See method DELETE-POS and method DELETE-KEY for the underlying mechanism.

:p

Return both the index/key and the value of the element, in the form of a Pair, and silently skip nonexistent elements:

my @tens = 0102030;
dd @tens[1]:p;        # OUTPUT: «1 => 10␤» 
dd @tens[042]:p;  # OUTPUT: «(0 => 0, 2 => 20)␤» 
 
my %month = Jan => 1Feb => 2Mar => 3;
dd %month<Feb>:p;          # OUTPUT: «"Feb" => 2␤» 
dd %month<Jan Foo Mar>:p;  # OUTPUT: «("Jan" => 1, "Mar" => 3)␤» 

If you don't want to skip nonexistent elements, use the negated form:

dd %month<Jan Foo Mar>:!p;  # OUTPUT: «("Jan" => 1, "Foo" => Any, "Mar" => 3)␤» 

Can be combined with the :exists and :delete adverbs.

See also the pairs routine.

:kv

Return both the index/key and the value of the element, in the form of a List, and silently skip nonexistent elements. When used on a slice, the return value is a single flat list of interleaved keys and values:

my @tens = 0102030;
dd @tens[1]:kv;        # OUTPUT: «(1, 10)␤» 
dd @tens[042]:kv;  # OUTPUT: «(0, 0, 2, 20)␤» 
 
my %month = Jan => 1Feb => 2Mar => 3;
dd %month<Feb>:kv;          # OUTPUT: «("Feb", 2)␤» 
dd %month<Jan Foo Mar>:kv;  # OUTPUT: «("Jan", 1, "Mar", 3)␤» 

If you don't want to skip nonexistent elements, use the negated form:

dd %month<Jan Foo Mar>:!kv;  # OUTPUT: «("Jan", 1, "Foo", Any, "Mar", 3)␤» 

This adverb is commonly used to iterate over slices:

for %month<Feb Mar>:kv -> $month$i {
    say "$month had {Date.new(2015$i1).days-in-month} days in 2015"
}

Can be combined with the :exists and :delete adverbs.

See also the kv routine.

:k

Return only the index/key of the element, rather than its value, and silently skip nonexistent elements:

my @tens = 0102030;
dd @tens[1]:k;        # OUTPUT: «1␤» 
dd @tens[042]:k;  # OUTPUT: «(0, 2)␤» 
 
my %month = Jan => 1Feb => 2Mar => 3;
dd %month<Feb>:k;          # OUTPUT: «"Feb"␤» 
dd %month<Jan Foo Mar>:k;  # OUTPUT: «("Jan", "Mar")␤» 

If you don't want to skip nonexistent elements, use the negated form:

dd %month<Jan Foo Mar>:!k;  # OUTPUT: «("Jan", "Foo", "Mar")␤» 

See also the keys routine.

:v

Return the bare value of the element (rather than potentially returning a mutable value container), and silently skip nonexistent elements:

my @tens = 0102030;
dd @tens[1]:v;        # OUTPUT: «10␤» 
dd @tens[042]:v;  # OUTPUT: «(0, 20)␤» 
@tens[3= 31;        # OK 
@tens[3]:v = 31;      # ERROR, Cannot modify an immutable Int (31) 
 
my %month = Jan => 1Feb => 2Mar => 3;
dd %month<Feb>:v;          # OUTPUT: «2␤» 
dd %month<Jan Foo Mar>:v;  # OUTPUT: «(1, 3)␤» 

If you don't want to skip nonexistent elements, use the negated form:

dd %month<Jan Foo Mar>:!v;  # OUTPUT: «(1, Any, 3)␤» 

See also the values routine.

Custom types

The subscripting interfaces described on this page are not meant to be exclusive to Perl 6's built-in collection types - you can (and should) reuse them for any custom type that wants to provide access to data by index or key.

You don't have to manually overload the postcircumfix [ ] and postcircumfix { } operators and re-implement all their magic, to achieve that - instead, you can rely on the fact that their standard implementation dispatches to a well-defined set of low-level methods behind the scenes. For example:

when you write: this gets called behind the scenes:
%foo<aa> %foo.AT-KEY("aa")
%foo<aa>:delete %foo.DELETE-KEY("aa")
@foo[3, 4, 5] @foo.AT-POS(3), @foo.AT-POS(4), @foo.AT-POS(5)
@foo[*-1] @foo.AT-POS(@foo.elems - 1)

So in order to make subscripting work, you only have to implement or delegate those low-level methods (detailed below) for your custom type.

If you do, you should also let your type compose the Positional or Associative role, respectively. This doesn't add any functionality per se, but announces (and may be used to check) that the type implements the corresponding subscripting interface.

Custom type example

Imagine a HTTP::Header type which, despite being a custom class with special behavior, can be indexed like a hash:

my $request = HTTP::Request.new(GET => "perl6.org");
say $request.header.^name;  # OUTPUT: «HTTP::Header␤» 
 
$request.header<Accept> = "text/plain";
$request.header{'Accept-' X~ <Charset Encoding Language>} = <utf-8 gzip en>;
$request.header.push('Accept-Language' => "fr");  # like .push on a Hash 
 
say $request.header<Accept-Language>.perl;  # OUTPUT: «["en", "fr"]␤» 
 
my $rawheader = $request.header.Str;  # stringify according to HTTP spec 

The simplest way to implement this class, would be to give it an attribute of type Hash, and delegate all subscripting and iterating related functionality to that attribute (using a custom type constraint to make sure users don't insert anything invalid into it):

class HTTP::Header does Associative does Iterable {
    subset StrOrArrayOfStr where Str | ( Array & {.all ~~ Str} );
 
    has %!fields of StrOrArrayOfStr
                 handles <AT-KEY EXISTS-KEY DELETE-KEY push
                          iterator list kv keys values>;
 
    method Str { #`[not shown, for brevity] }
}

However, HTTP header field names are supposed to be case-insensitive (and preferred in camel-case). We can accommodate this by taking the *-KEY and push methods out of the handles list, and implementing them separately like this:

method AT-KEY     ($keyis rw { %!fields{normalize-key $key}        }
method EXISTS-KEY ($key)       { %!fields{normalize-key $key}:exists }
method DELETE-KEY ($key)       { %!fields{normalize-key $key}:delete }
method push(*@_{ #`[not shown, for brevity] }
 
sub normalize-key ($key{ $key.subst(/\w+/*.tc:g}

Note that subscripting %!fields returns an appropriate rw container, which our AT-KEY can simply pass on.

However, we may prefer to be less strict about user input and instead take care of sanitizing the field values ourselves. In that case, we can remove the StrOrArrayOfStr type constraint on %!fields, and replace our AT-KEY implementation with one that returns a custom Proxy container which takes care of sanitizing values on assignment:

multi method AT-KEY (::?CLASS:D: $keyis rw {
    my $element := %!fields{normalize-key $key};
 
    Proxy.new(
        FETCH => method () { $element },
 
        STORE => method ($value{
            $element = do given $value».split(/',' \s+/).flat {
                when 1  { .[0}    # a single value is stored as a string 
                default { .Array }  # multiple values are stored as an array 
            }
        }
    );
}

Note that declaring the method as multi and restricting it to :D (defined invocants) makes sure that the undefined case is passed through to the default implementation provided by Any (which is involved in auto-vivification).

Methods to implement for positional subscripting

In order to make index-based subscripting via postcircumfix [ ] work for your custom type, you should implement at least elems, AT-POS and EXISTS-POS - and optionally others as detailed below.

method elems

multi method elems(::?CLASS:D:)

Expected to return a number indicating how many subscriptable elements there are in the object. May be called by users directly, and is also called by postcircumfix [ ] when indexing elements from the end, as in @foo[*-1].

If not implemented, your type will inherit the default implementation from Any that always returns 1 for defined invocants - which is most likely not what you want. So if the number of elements cannot be known for your positional type, add an implementation that fails or dies, to avoid silently doing the wrong thing.

method AT-POS

multi method AT-POS (::?CLASS:D: $index)

Expected to return the element at position $index. This is what postcircumfix [ ] normally calls.

If you want an element to be mutable (like they are for the built-in Array type), you'll have to make sure to return it in the form of an item container that evaluates to the element's value when read, and updates it when assigned to. (Remember to use return-rw or the is rw routine trait to make that work; see the example.)

method EXISTS-POS

multi method EXISTS-POS (::?CLASS:D: $index)

Expected to return a Bool indicating whether or not there is an element at position $index. This is what postcircumfix [ ] calls when invoked like @foo[42]:exists.

What "existence" of an element means, is up to your type.

If you don't implement this, your type will inherit the default implementation from Any, which returns True for 0 and False for any other index - which is probably not what you want. So if checking for element existence cannot be done for your type, add an implementation that fails or dies, to avoid silently doing the wrong thing.

method DELETE-POS

multi method DELETE-POS (::?CLASS:D: $index)

Expected to delete the element at position $index, and return the value it had. This is what postcircumfix [ ] calls when invoked like @foo[42]:delete.

What "deleting" an element means, is up to your type.

Implementing this method is optional; if you don't, users trying to delete elements from an object of this type will get an appropriate error message.

method ASSIGN-POS

multi method ASSIGN-POS (::?CLASS:D: $index$new)

Expected to set the element at position $index to the value $new. Implementing this is entirely optional; if you don't, self.AT-POS($index) = $new is used instead, and if you do, you should make sure it has the same effect.

This is meant as an opt-in performance optimization, so that simple assignments like @numbers[5] = "five" can operate without having to call AT-POS (which would have to create and return a potentially expensive container object).

Note that implementing ASSIGN-POS does not relieve you from making AT-POS an rw method though, because less trivial assignments/modifications such as @numbers[5]++ will still use AT-POS.

method BIND-POS

multi method BIND-POS (::?CLASS:D: $index, \new)

Expected to bind the value or container new to the slot at position $index, replacing any container that would be naturally found there. This is what is called when you write:

my $x = 10;
@numbers[5:= $x;

The generic Array class supports this in order to allow building complex linked data structures, but for more domain-specific types it may not make sense, so don't feel compelled to implement it. If you don't, users will get an appropriate error message when they try to bind to a positional slot of an object of this type.

Methods to implement for associative subscripting

In order to make key-based subscripting via postcircumfix { } work for your custom type, you should implement at least AT-KEY and EXISTS-KEY - and optionally others as detailed below.

method AT-KEY

multi method AT-KEY (::?CLASS:D: $key)

Expected to return the element associated with $key. This is what postcircumfix { } normally calls.

If you want an element to be mutable (like they are for the built-in Hash type), you'll have to make sure to return it in the form of an item container that evaluates to the element's value when read, and updates it when assigned to. (Remember to use return-rw or the is rw routine trait to make that work; see the example.)

On the other hand if you want your collection to be read-only, feel free to return non-container values directly.

method EXISTS-KEY

multi method EXISTS-KEY (::?CLASS:D: $key)

Expected to return a Bool indicating whether or not there is an element associated with $key. This is what postcircumfix { } calls when invoked like %foo<aa>:exists .

What "existence" of an element means, is up to your type.

If you don't implement this, your type will inherit the default implementation from Any, which always returns False - which is probably not what you want. So if checking for element existence cannot be done for your type, add an implementation that fails or dies, to avoid silently doing the wrong thing.

method DELETE-KEY

multi method DELETE-KEY (::?CLASS:D: $key)

Expected to delete the element associated with $key, and return the value it had. This is what postcircumfix { } calls when invoked like %foo<aa>:delete .

What "deleting" an element means, is up to your type - though it should usually cause EXISTS-KEY to become False for that key.

Implementing this method is optional; if you don't, users trying to delete elements from an object of this type will get an appropriate error message.

method ASSIGN-KEY

multi method ASSIGN-KEY (::?CLASS:D: $key$new)

Expected to set the element associated with $key to the value $new. Implementing this is entirely optional; if you don't, self.AT-KEY($key) = $new is used instead, and if you do, you should make sure it has the same effect.

This is meant as an opt-in performance optimization, so that simple assignments %age<Claire> = 29 can operate without having to call AT-KEY (which would have to create and return a potentially expensive container object).

Note that implementing ASSIGN-KEY does not relieve you from making AT-KEY an rw method though, because less trivial assignments/modifications such as %age<Claire>++ will still use AT-KEY.

method BIND-KEY

multi method BIND-KEY (::?CLASS:D: $key, \new)

Expected to bind the value or container new to the slot associated with $key, replacing any container that would be naturally found there. This is what is called when you write:

my $x = 10;
%age<Claire> := $x;

The generic Hash class supports this in order to allow building complex linked data structures, but for more domain-specific types it may not make sense, so don't feel compelled to implement it. If you don't, users will get an appropriate error message when they try to bind to an associative slot of an object of this type.