In Array§

See primary documentation in context for method of

method of()

Returns the type constraint for the values of the invocant. By default, i.e. if no type constraint is given during declaration, the method returns (Mu).

my @a1 = 1'two'3.14159;              # (no type constraint specified) 
say @a1.of;                              # OUTPUT: «(Mu)␤» 
 
my Int @a2 = 123;                    # (values must be of type Int) 
say @a2.of;                              # OUTPUT: «(Int)␤» 
@a2.push: 'd';
CATCH { default { put .^name''.Str } };
# OUTPUT: «X::TypeCheck::Assignment: Type check failed in assignment to @a2; expected Int but got Str ("d")␤»

In Variable§

See primary documentation in context for trait of

multi sub trait_mod:<of>(Mu:U $targetMu:U $type)

Sets the type constraint of a container bound to a variable.

my $i of Int = 42;
$i = "forty plus two";
CATCH { default { say .^name' '.Str } }
# OUTPUT: «X::TypeCheck::Assignment Type check failed in assignment to $i; expected Int but got Str ("forty plus two")␤»

You can use any value defined in compile time as a type constraint, including constants:

constant \T = Int;
my $i of T = 42;

which would be equivalent to the previous definition.

In role QuantHash§

See primary documentation in context for method of

method of()

Returns the type of value a value of this QuantHash may have. This is typically Bool for Setty, UInt for Baggy or Real for Mixy roles.

In Scalar§

See primary documentation in context for method of

method of(Scalar:D: --> Mu)

Returns the type constraint of the container.

Example:

my Cool $x = 42;
say $x.VAR.of;                  # OUTPUT: «(Cool)␤»

In role Associative§

See primary documentation in context for method of

method of()

Associative, as the definition above shows, is actually a parameterized role which can use different classes for keys and values. As seen at the top of the document, by default it coerces the key to Str and uses a very generic Mu for value.

my %any-hash;
say %any-hash.of# OUTPUT: «(Mu)␤»

The value is the first parameter you use when instantiating Associative with particular classes:

class DateHash is Hash does Associative[Cool,DateTime{};
my %date-hash := DateHash.new;
say %date-hash.of# OUTPUT: «(Cool)␤»

In Code§

See primary documentation in context for method of

method of(Code:D: --> Mu)

Returns the return type constraint of the Code:

say -> () --> Int {}.of# OUTPUT: «(Int)␤»

In role Positional§

See primary documentation in context for method of

method of()

Returns the type constraint for elements of the positional container, that is, the T in the definition above, which, as it can be seen, defaults to Mu. It is returned as a type object.

my @þ;
say @þ.of.^name;   # OUTPUT: «Mu␤ 
my Str @þð;
say @þð.of.raku;   # OUTPUT: «Str␤» 
say (my int @).of# OUTPUT: «(int)␤» 

In Hash§

See primary documentation in context for method of

method of(Hash:D:)

Returns the type constraint for the values of the invocant. By default, i.e., if no type constraint is given during declaration, the method returns (Mu).

my %h1 = 'apples' => 3'oranges' => 7;  # (no type constraint specified) 
say %h1.of;                              # OUTPUT: «(Mu)␤» 
 
my Int %h2 = 'oranges' => 7;             # (values must be of type Int) 
say %h2.of;                              # OUTPUT: «(Int)␤»