I'd like to create some parametrized types for Raku; basically, I'd like to create some different classes whose main difference would be the range of values of one of its attributes; for instance, classes represent types of building, I'd like to have different classes for buildings with 3 or any other number of floors. So this is the best I could think of:
subset Two-Tops of UInt where * <=2;
subset Three-Tops of UInt where * <=3;
role Zipi[ ::Capper ] {
has Capper $.floor;
}
class Capped-at-three does Zipi[Three-Tops] {}
my $capped = Capped-at-three.new( floor => 2 );
say $capped.raku;
This is clearly unpractical as soon as you need to take care of many different numbers of floors (not here in Granada, where they have at most 10, I think, but well... ). The problem here is basically you need to have the information for subsets at compile time, so unless you use macros (still experimental), there's no way you can use any kind of variable. So can you think of a practical way of defining this kind of curried roles for any value of the parameter?
Actually, unlike I said in my previous you can use conditions in where clauses without problem, you just need to encase them in braces:
role Zipi[$condition] {
has $.floor is rw where {$_ ~~ $condition}
method foo($x) { $!floor = $x }
}
class A does Zipi[2 < * < 5] {
method bar($x) { $.floor = $x }
}
#my $a = A.new( floor => 10); # error
my $a = A.new( floor => 4); # OK
#$a.foo(10); # error
$a.foo(3); # OK
#$a.bar(0); # error
$a.bar(4); # OK
#$a.floor = 9; # error
$a.floor = 3; # OK
That should cover all of the assignment types
This does not require high magic, so sorry, @raiph, I've chosen it. Thanks!
.oO ( I'm glad @jj got & accepted this solution and not mine!) What?!? Who said that? I encountered the same issue with
where
clauses when using*
but found it worked using braces and~~
. But I didn't realize that that's what you meant by "I tried using anonymouswhere
clauses". Maybe you read my answer and saw that option and it inspired the above? In which case my aspirational "step in the right direction" applies, and in turn the sense that it might inspire us all to stay ahead of dreaded lurchers, and know we are in this together, and share the desire to spread what we have... ;)@raiph: ps, check out reddit — would love some feedback on my post there re CLDR (def value your input/observations)
Heh. I just posted a comment there, then visited here and have just seen your comment for the first time. Maybe it's a zombie power that I read your mind without realizing I'd done so?
@raiph I'm not so totally sure about this now. At the end of the day, it needs runtime constants. It's a bit more flexible. So will take a second look at your answer, and maybe switch again.