Types, classes & roles
The type tower & introspection
FullAsk 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
True42 ~~ Int is True (42 is an Int); Int ~~ Numeric is True because Int does the Numeric role — membership follows the hierarchy.
Notes #
.WHATgives a type object — an "empty" instance of the type.Intand42.WHATare the same object; comparing with===confirms it.- The
.^namemethod is a metamethod (the.^calls into the metaobject); the metaobject is also where.^methods,.^attributes, and.^mrolive. - Numeric types form a tower
Int⊂Rat/FatRat⊂Num⊂Complex, all doing theNumericrole — see Rational literals for how values move through it.