Types, classes & roles

Subsets

Partial

Named types that narrow another with a where constraint (not yet enforced in Raku++).

A subset is a named type built from an existing one plus a where predicate. It lets you attach a validity rule to a type and reuse it in signatures and declarations.

Declaring and using a subset #

subset Even of Int where * %% 2;
my Even $n = 4;
say $n;
Output
4

Even is any Int for which * %% 2 (divisible by two) holds. Assigning 4 succeeds.

Gap: a subset's where predicate is not yet enforced in Raku++ — assigning (or passing) an odd number should be a type-check failure (as in Rakudo) but is currently accepted. The base type (of Int) is checked; only the where part is skipped. Note this differs from a direct parameter where constraint, which is enforced.

Notes #