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'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'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>> # In the Raku REPL (wamba's enum example 'Month')<br>>=
; enum Month (jan =3D> 1, |<feb mar apr may jun jul aug sep oct nov d=
ec>);<br>Map.new((apr =3D> 4, aug =3D> 8, dec =3D> 12, feb =3D&=
gt; 2, jan =3D> 1, jul =3D> 7, jun =3D> 6, mar =3D> 3, may =3D&=
gt; 5, nov =3D> 11, oct =3D> 10, sep =3D> 9))<br>><br>> Mont=
h.^enum_values<br>{apr =3D> 4, aug =3D> 8, dec =3D> 12, feb =3D>=
; 2, jan =3D> 1, jul =3D> 7, jun =3D> 6, mar =3D> 3, may =3D>=
; 5, nov =3D> 11, oct =3D> 10, sep =3D> 9}<br>> Month.^enum_val=
ue_list<br>(jan feb mar apr may jun jul aug sep oct nov dec)<br>><br>>=
; Month.^enum_keys<br>No such method 'enum_keys' for invocant of ty=
pe<br>'Perl6::Metamodel::EnumHOW'<br>=C2=A0 in block <unit> a=
t <unknown file> line 4<br><br>> Month.^enum_key_list<br>No such m=
ethod 'enum_key_list' for invocant of type<br>'Perl6::Metamodel=
::EnumHOW'<br>=C2=A0 in block <unit> at <unknown file> line=
1<br><br>><br>> #Random keyword attempts:<br>> Month.key<br>Invoc=
ant of method 'key' must be an object instance of type 'Mu'=
, not a<br>type object of type 'Month'.=C2=A0 Did you forget a '=
;.new'?<br>=C2=A0 in block <unit> at <unknown file> line 1<=
br><br>> Month>>.key<br>Invocant of method 'key' must be a=
n object instance of type 'Mu', not a<br>type object of type 'M=
onth'.=C2=A0 Did you forget a '.new'?<br>=C2=A0 in block <un=
it> at <unknown file> line 1<br><br>> Month.keys<br>()<br>> =
Month>>.keys<br>(())</div>
--000000000000744ef205c198203d--