Correct enum incantation?

2021-05-05 Thread William Michels via perl6-language
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, |);
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  at  line 4

> Month.^enum_key_list
No such method 'enum_key_list' for invocant of type
'Perl6::Metamodel::EnumHOW'
  in block  at  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  at  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  at  line 1

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


Re: Correct enum incantation?

2021-05-05 Thread William Michels via perl6-language
Thank you Fernando!

(still on Rakudo 2020.10 here).

On Wed, May 5, 2021 at 10:24 AM Fernando Santagata <
nando.santag...@gmail.com> wrote:

> On Wed, May 5, 2021 at 6:00 PM William Michels via perl6-users <
> perl6-us...@perl.org> wrote:
>
>> 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`.
>>
>
> Probably you need the new and shiny compiler ;)
>
> > $*RAKU.compiler.version
> v2021.04
> > Month.keys
> (oct dec aug jun mar apr feb nov jul may sep jan)
> > Month.values
> (11 6 9 2 5 7 3 12 8 4 10 1)
> > .say for Month.kv
> nov
> 11
> jul
> 7
> sep
> 9
> jan
> 1
> oct
> 10
> mar
> 3
> jun
> 6
> apr
> 4
> aug
> 8
> dec
> 12
> feb
> 2
> may
> 5
> --
> Fernando Santagata
>


Re: Correct enum incantation?

2021-05-05 Thread Fernando Santagata
On Wed, May 5, 2021 at 6:00 PM William Michels via perl6-users <
perl6-us...@perl.org> wrote:

> 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`.
>

Probably you need the new and shiny compiler ;)

> $*RAKU.compiler.version
v2021.04
> Month.keys
(oct dec aug jun mar apr feb nov jul may sep jan)
> Month.values
(11 6 9 2 5 7 3 12 8 4 10 1)
> .say for Month.kv
nov
11
jul
7
sep
9
jan
1
oct
10
mar
3
jun
6
apr
4
aug
8
dec
12
feb
2
may
5
-- 
Fernando Santagata