Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can subsets be exported in Raku?

I'd like to define a few subsets to which I am also adding a few constrains and some die statements for some useful error messages. I don't want to define them at the top of the module that uses those subsets and instead want to place them in another module while also doing away with using their fully qualified names (FQNs). For instance, I have

unit module Long::Module::Subsets;

subset PosInt
where ($_ ~~ Int || "The value must be an integer")
   && ($_ > 0    || "The value must be greater than 0")
is export
;

# other subsets ...

but got

===SORRY!=== Error while compiling /tmp/637321813/main.pl6
Two terms in a row ...

That not working I figured I could instead do something as follows but I'm wondering If I could avoid doing it:

use Long::Module::Subsets;

unit Long::Module;

my constant PosInt = Long::Module::Subsets::PosInt;
my constant Byte   = Long::Module::Subsets::Byte;
# ... more subsets here

# ... some code here

my PosInt $age;
like image 577
Luis F. Uceta Avatar asked Jan 24 '20 23:01

Luis F. Uceta


1 Answers

Subsets can indeed be exported. The problem here is that the is export trait isn't properly applied to the PosInt subset (and any other subset you might've also wished to export); the trait must be applied immediately after the new type being defined and right before any constrains introduced with where. By applying the trait correctly:

unit module Long::Module::Subsets;

subset PosInt is export
where ($_ ~~ Int || "The value must be an integer")
   && ($_ > 0    || "The value must be greater than 0")
;

# other subsets ...

the following should work fine:

use Long::Module::Subsets;

unit Long::Module;

# ... some code here

my PosInt $age;
like image 127
Luis F. Uceta Avatar answered Oct 17 '22 08:10

Luis F. Uceta