Reputation: 619
I would like to construct a String-typed enum. The following works:
my Str enum E ( <a b c> Z=> 'one', 'two', 'three' );
E.kv.raku.say;
("c", "three", "a", "one", "b", "two").Seq
However, trying the following does not:
my Str @a = <a b c>;
my Str @b = <one two three>;
my Str enum F ( @a Z=> @b );
F.kv.raku.say;
No values supplied to enum (does @a Z=> @b need to be declared constant?)
Is this not supported?
Raku/roast covers enum construction like in E
but I didn't see any test cases for F
.
Out of curiosity, I also tried:
my $a = <a b c>;
my $b = <one two three>;
my Str enum G ( $a<> Z=> $b<>.map( { .Str } ) );
G.kv.raku.say;
("", "").Seq
Upvotes: 9
Views: 147
Reputation: 2341
Can you let us know what Rakudo version? WithRakudo™ v2022.07
and within the Raku REPL, I see virtually identical returns (i.e. no error):
~$ raku
Welcome to Rakudo™ v2022.07.
Implementing the Raku® Programming Language v6.d.
Built on MoarVM version 2022.07.
To exit type 'exit' or '^D'
[0] > my Str enum E ( <a b c> Z=> 'one', 'two', 'three' );
Map.new((a => one, b => two, c => three))
[1] > E.kv.raku.say;
("a", "one", "c", "three", "b", "two").Seq
[1] > my Str @a = <a b c>;
[a b c]
[2] > my Str @b = <one two three>;
[one two three]
[3] > my Str enum F ( @a Z=> @b );
Map.new((a => one, b => two, c => three))
[4] > F.kv.raku.say;
("b", "two", "a", "one", "c", "three").Seq
Upvotes: 1
Reputation: 29454
The warning asks:
does @a Z=> @b need to be declared constant
And it's correct; since an enum
is a compile-time declaration, everything involved with computing its keys and values must be available at compile time. Declaring @a
and @b
as constant
s resolves the problem. Thus:
my constant @a = <a b c>;
my constant @b = <one two three>;
my Str enum F ( @a Z=> @b );
F.kv.raku.say;
Produces:
("a", "one", "c", "three", "b", "two").Seq
Upvotes: 9