Types, classes & roles

The type tower & introspection

Full

Ask any value its type with .WHAT / .^name, and test membership with ~~.

Every value has a type, and every type sits in a hierarchy — the "type tower" — rooted at Mu. A handful of universal methods let you ask a value what it is and test whether it belongs to a type.

Asking a value its type #

.WHAT returns the value's type object (which prints in parentheses); .^name returns the type's name as a string.

say 42.WHAT;
say 42.^name;
say (1, 2).WHAT;
Output
(Int)
Int
(List)

Type membership — ~~ #

Smartmatch against a type asks "is this value of that type (or a subtype)?" Because the tower is nested, an Int is also Numeric.

say 42 ~~ Int;
say Int ~~ Numeric;
Output
True
True

42 ~~ Int is True (42 is an Int); Int ~~ Numeric is True because Int does the Numeric role — membership follows the hierarchy.

Notes #