Should Raku typed enums work with custom types? I get compile errors when trying the following:
role A { }
class B does A { }
class C does A { }
my A enum E ( b => B, c => C );
results in
Cannot find method 'mro' on object of type Perl6::Metamodel::ParametricRoleGroupHOW
and
class D { }
my D enum F ( b => D.new, c => D.new );
results in
Cannot auto-generate a proto method for 'Int' in the setting
or does this fall under
Complex expressions for generating key-value pairs are not supported.
EDIT
Regarding the first example - it looks like Raku doesn't like roles as the type constraint. The same error is given when trying, e.g.
my Rational enum G ( one => 1/1, two => 1/2 );
and as for what I was really hoping for:
Using a type object as a value for an enum not yet implemented. Sorry.
The problem is most like that only Int
and Str
are actually tested thoroughly.
For example, your Rational
one wouldn't even work correctly if you had used Rat
instead.
my Rat enum G ( one => 1/1, two => 1/2 );
say one.Rat;
# one
That should say 1
to be consistent with Int
enums and .Int
, and to Str
enums and .Str
my Int enum I ( one-i => 1, two-i => 2 );
my Str enum S ( one-s => 'ONE', two-s => 'TWO' );
say one-i.Int; # 1
say one-s.Str; # ONE
So the reason for this error:
Cannot auto-generate a proto method for 'Int' in the setting
Is that Rakudo assumes that anything other than a Str
enum must be an Int
enum.
What it should do is generate a method that is the same name as the class that returns a value that isn't boxed by the enum.
If it doesn't even work correctly for Rat
, which compiles and is a built-in type; then a user defined one doesn't stand a chance. That applies doubly to a role.
It is a bug. (actually at least two)
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