Correct enum incantation?

[email protected] (William Michels via perl6-language) Wed, 5 May 2021 09:59:31 -0700
Newsgroups perl.perl6.language
Message-ID <CAA99HCxxmbjuRjyAsvBCxvY1q1LN03c+jS1k+LFe-HjJvoVt_g@mail.gmail.com>
--000000000000744ef205c198203d
Content-Type: text/plain; charset="UTF-8"

Hello,

I've been reading over an interesting Answer on StackOverflow by wamba:

https://stackoverflow.com/a/67324175/7270649

I started trying to explore enums on my own and quickly realized that
method calls on enums are different from simple key/value pairs. For enums,
calling a `.key` or `.value` or `.kv` method won't work. Instead one must
use something like `.^enum_values` or `.^enum_value_list`.

Can someone explain how `.^enum_values` or `.^enum_value_list` came to be
the **correct** incantation for enums, and why there is no equivalent
`.^enum_keys` or `.^enum_key_list` commands? Thank you, Bill.


> # In the Raku REPL (wamba's enum example 'Month')
> enum Month (jan => 1, |<feb mar apr may jun jul aug sep oct nov dec>);
Map.new((apr => 4, aug => 8, dec => 12, feb => 2, jan => 1, jul => 7, jun
=> 6, mar => 3, may => 5, nov => 11, oct => 10, sep => 9))
>
> Month.^enum_values
{apr => 4, aug => 8, dec => 12, feb => 2, jan => 1, jul => 7, jun => 6, mar
=> 3, may => 5, nov => 11, oct => 10, sep => 9}
> Month.^enum_value_list
(jan feb mar apr may jun jul aug sep oct nov dec)
>
> Month.^enum_keys
No such method 'enum_keys' for invocant of type
'Perl6::Metamodel::EnumHOW'
  in block <unit> at <unknown file> line 4

> Month.^enum_key_list
No such method 'enum_key_list' for invocant of type
'Perl6::Metamodel::EnumHOW'
  in block <unit> at <unknown file> line 1

>
> #Random keyword attempts:
> Month.key
Invocant of method 'key' must be an object instance of type 'Mu', not a
type object of type 'Month'.  Did you forget a '.new'?
  in block <unit> at <unknown file> line 1

> Month>>.key
Invocant of method 'key' must be an object instance of type 'Mu', not a
type object of type 'Month'.  Did you forget a '.new'?
  in block <unit> at <unknown file> line 1

> Month.keys
()
> Month>>.keys
(())

--000000000000744ef205c198203d
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Hello,<br><br>I&#39;ve been reading over an interesting An=
swer on StackOverflow by wamba:<br><br><a href=3D"https://stackoverflow.com=
/a/67324175/7270649" target=3D"_blank">https://stackoverflow.com/a/67324175=
/7270649</a><br><br>I started trying to explore enums on my own and quickly=
 realized that method calls on enums are different from simple key/value pa=
irs. For enums, calling a `.key` or `.value` or `.kv` method won&#39;t work=
. Instead one must use something like `.^enum_values` or `.^enum_value_list=
`.<br><br>Can someone explain how `.^enum_values` or `.^enum_value_list` ca=
me to be the **correct** incantation for enums, and why there is no equival=
ent `.^enum_keys` or `.^enum_key_list` commands? Thank you, Bill.<br><br><b=
r>&gt; # In the Raku REPL (wamba&#39;s enum example &#39;Month&#39;)<br>&gt=
; enum Month (jan =3D&gt; 1, |&lt;feb mar apr may jun jul aug sep oct nov d=
ec&gt;);<br>Map.new((apr =3D&gt; 4, aug =3D&gt; 8, dec =3D&gt; 12, feb =3D&=
gt; 2, jan =3D&gt; 1, jul =3D&gt; 7, jun =3D&gt; 6, mar =3D&gt; 3, may =3D&=
gt; 5, nov =3D&gt; 11, oct =3D&gt; 10, sep =3D&gt; 9))<br>&gt;<br>&gt; Mont=
h.^enum_values<br>{apr =3D&gt; 4, aug =3D&gt; 8, dec =3D&gt; 12, feb =3D&gt=
; 2, jan =3D&gt; 1, jul =3D&gt; 7, jun =3D&gt; 6, mar =3D&gt; 3, may =3D&gt=
; 5, nov =3D&gt; 11, oct =3D&gt; 10, sep =3D&gt; 9}<br>&gt; Month.^enum_val=
ue_list<br>(jan feb mar apr may jun jul aug sep oct nov dec)<br>&gt;<br>&gt=
; Month.^enum_keys<br>No such method &#39;enum_keys&#39; for invocant of ty=
pe<br>&#39;Perl6::Metamodel::EnumHOW&#39;<br>=C2=A0 in block &lt;unit&gt; a=
t &lt;unknown file&gt; line 4<br><br>&gt; Month.^enum_key_list<br>No such m=
ethod &#39;enum_key_list&#39; for invocant of type<br>&#39;Perl6::Metamodel=
::EnumHOW&#39;<br>=C2=A0 in block &lt;unit&gt; at &lt;unknown file&gt; line=
 1<br><br>&gt;<br>&gt; #Random keyword attempts:<br>&gt; Month.key<br>Invoc=
ant of method &#39;key&#39; must be an object instance of type &#39;Mu&#39;=
, not a<br>type object of type &#39;Month&#39;.=C2=A0 Did you forget a &#39=
;.new&#39;?<br>=C2=A0 in block &lt;unit&gt; at &lt;unknown file&gt; line 1<=
br><br>&gt; Month&gt;&gt;.key<br>Invocant of method &#39;key&#39; must be a=
n object instance of type &#39;Mu&#39;, not a<br>type object of type &#39;M=
onth&#39;.=C2=A0 Did you forget a &#39;.new&#39;?<br>=C2=A0 in block &lt;un=
it&gt; at &lt;unknown file&gt; line 1<br><br>&gt; Month.keys<br>()<br>&gt; =
Month&gt;&gt;.keys<br>(())</div>

--000000000000744ef205c198203d--