Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

raku typed enums not working with custom types

Tags:

enums

raku

typed

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.

like image 953
user1915829 Avatar asked Jun 23 '21 15:06

user1915829


1 Answers

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.


TL;DR

It is a bug. (actually at least two)

like image 77
Brad Gilbert Avatar answered Nov 07 '22 16:11

Brad Gilbert