Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parametrized subsets

Tags:

types

raku

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?

like image 303
jjmerelo Avatar asked Mar 20 '19 16:03

jjmerelo


1 Answers

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
like image 122
Jonathan Worthington Avatar answered Sep 22 '22 01:09

Jonathan Worthington