As far as I understood it, you can only parametrize subsets by values itself
subset MoreThanZero where * > 0
But, is there a direct way of implementing something like this?
subset MoreThan[\x] where * > x
And then declare
my MoreThan[1000] $thousand-plus
Maybe a roundabout way would be to use parameterized roles, but I was thinking about a more direct approach. Is there one?
Probably the easiest option is to create a type that provides a custom parameterize
method, and then using the MOP to construct a subset type based on that:
class MoreThan {
method ^parameterize(Mu, $limit) {
Metamodel::SubsetHOW.new_type:
name => "more than $limit",
refinee => Numeric,
refinement => * > $limit
}
}
Then this:
my MoreThan[0] $x = 1;
say $x;
my MoreThan[2] $y = 3;
say $y;
$y = 1;
Produces:
1
3
Type check failed in assignment to $y; expected more than 2 but got Int (1)
in block <unit> at ss.p6 line 14
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With