4 ms·
Indexing into a List sub lookup ( List \xs, Int \index ){…} Make sure the index is valid. sub lookup ( List \xs, Int \index where 0 ..^ xs.elems ){…}
by b2gills 8y ago
Indexing into a List
sub lookup ( List \xs, Int \index ){…}
Make sure the index is valid.
sub lookup ( List \xs, Int \index where 0 ..^ xs.elems ){…}
The `where` clause gets attached to the `Int` typecheck.
---
I think it may also apply if you have other limitations that you need to check regularly, like making sure that strings fit into a database.
sub foo ( Str \name where .chars ≤ 256 ){…}
Which Perl6 has the ability to give that a name as if it was a type so that it can be reused.
subset DB-Str of Str where .chars ≤ 256;
sub foo ( DB-Str \name ){…}
my DB-Str $name = '';
$name = 'a' x 10000;
# Typecheck error
---
One use of this feature is to implement the UInt type (This is almost exactly how it is implemented in the Rakudo implementation of Perl6)
subset UInt of Int where {$_ >= 0};
---
Then again I've never read any that talk about Perl6 as already having that feature; so maybe I'm mistaken.
I would guess that they are talking about something slightly different. (That often seems to be the case)