Literals & quoting

Allomorphs — IntStr, RatStr & friends

Full

Angle-bracket literals that are a number and a string at the same time.

A value written inside angle brackets — <42>, <1.5> — is an allomorph: it is both a number and the string you typed, at once. The <…> quoting word recognises a numeric word and gives back a dual-typed value (IntStr, RatStr, NumStr, ComplexStr) that behaves as the number in numeric context and as the string in string context. This is the type you get from user input parsed with <…>, from .words over numeric text, and from command-line arguments.

A number and a string at once #

<42> is an IntStr. It compares equal to the Int 42 and to the string "42", and it carries string methods like .flip.

my $n = <42>;
say $n.^name;
say $n + 8;
say $n.flip;
Output
IntStr
50
24

The type name IntStr says it all: it does the Int role and the Str role together. $n + 8 uses its Int half; $n.flip (reverse the characters) uses its Str half and gives back "24".

Each numeric kind has its allomorph #

A decimal gives a RatStr, and the value still works as an ordinary Rat in arithmetic.

say <1.5>.^name;
for <1 2.5 3> {
    say "$_ is {.^name}";
}
Output
RatStr
1 is IntStr
2.5 is RatStr
3 is IntStr

Iterating a <…> list yields an allomorph per word, each typed by what it looks like — the integers become IntStr, the decimal becomes RatStr.

Comparing both ways #

Because an allomorph carries both values, it matches numerically with == and stringwise with eq.

say <42> == 42;
say <42> eq "42";
Output
True
True

Notes #