raku typed enums not working with custom types
Asked Answered
H

1

10

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.

Herophilus answered 23/6, 2021 at 15:56 Comment(1)
This link says "All keys have to be of the same type". I'm no Raku expert, so I'm not certain that's the problem, but it seems likely?Cards
T
5

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)

Tufa answered 24/6, 2021 at 0:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.