keyof
ErrorsCollection

keyof

Synthesised documentation from type/Associative type/QuantHash type/Hash

From type/Associative

See Original text in context

method keyof()

Returns the parameterized key used for the Associative role, which is Any coerced to Str by default. This is the class used as second parameter when you use the parameterized version of Associative.

my %any-hash;
%any-hash.keyof# OUTPUT: «(Str(Any))␤»

From type/QuantHash

See Original text in context

method keyof()

Returns the type of value a key of this subclass of QuantHash may have. This is typically Mu, which is also the default for punned QuantHashes.

From type/Hash

See Original text in context

method keyof()

Returns the type constraint for the keys of the invocant. For normal hashes the method returns the coercion type (Str(Any)) while for non-string keys hashes the type used in the declaration of the Hash is returned.

my %h1 = 'apples' => 3'oranges' => 7;  # (no key type specified) 
say %h1.keyof;                           # OUTPUT: «(Str(Any))␤» 
 
my %h2{Str} = 'oranges' => 7;            # (keys must be of type Str) 
say %h2.keyof;                           # (Str) 
%h2{3} = 'apples';                       # throws exception 
CATCH { default { put .^name''.Str } };
# OUTPUT: «X::TypeCheck::Binding: Type check failed in binding to key; expected Str but got Int (3)␤» 
 
my %h3{Int};                             # (this time, keys must be of type Int) 
%h3{42} = 4096;
say %h3.keyof;                           # (Int)