The of routine

Combined from primary sources listed below.

In class Code ( Type/Code )§

See primary docmentation in context for method of.

method of§

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

Returns the return type constraint of the Code:

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

In class Scalar ( Type/Scalar )§

See primary docmentation in context for method of.

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 class Variable ( Type/Variable )§

See primary docmentation in context for trait of.

trait of§

multi trait_mod:<of>(Mu:U $target, Mu: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 class Hash ( Type/Hash )§

See primary docmentation in context for method of.

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)␤»

In class Array ( Type/Array )§

See primary docmentation in context for method of.

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 = 1, 2, 3;                    # (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 role QuantHash ( Type/QuantHash )§

See primary docmentation in context for method of.

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 role Associative ( Type/Associative )§

See primary docmentation in context for method of.

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 role Positional ( Type/Positional )§

See primary docmentation in context for method of.

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)␤»