develooper Front page | perl.perl6.language | Postings from May 2021

Correct enum incantation?

Thread Next
From:
William Michels via perl6-language
Date:
May 5, 2021 17:00
Subject:
Correct enum incantation?
Message ID:
CAA99HCxxmbjuRjyAsvBCxvY1q1LN03c+jS1k+LFe-HjJvoVt_g@mail.gmail.com
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
(())

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About