Skip to content

Commit 02c8978

Browse files
committed
Document that an enum class can be considered a Map
For the purpose of calling keys, values, kv, pairs, antipairs, invert methods.
1 parent 20f577e commit 02c8978

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

doc/Type/Enumeration.rakudoc

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,34 @@ enum Foo <bar baz>;
7171
say baz ~~ Foo; # OUTPUT: «True␤»
7272
say Foo ~~ bar; # OUTPUT: «False␤»
7373

74-
=head1 Methods
74+
=head1 Methods that work on the enum class
75+
76+
As of release 2021.04 of the Rakudo compiler, an enum class can be
77+
considered as an instantiated L<Map|/type/Map> object. This means
78+
you can use the
79+
L<keys|type/Map#method_keys>,
80+
L<values|type/Map#method_values>,
81+
L<kv|type/Map#method_kv>,
82+
L<pairs|type/Map#method_pairs>,
83+
L<antipairs|type/Map#method_antipairs>,
84+
L<invert|type/Map#method_invert>
85+
on an enum class and get the expected result.
7586

76-
These are the methods included in this role:
87+
enum Norse-gods <Þor Oðin Freija>;
88+
say Nors-gods.keys; # OUTPUT: «(Þor Oðin Freija)␤»
89+
90+
=head2 method enums
91+
92+
method enums()
93+
94+
Returns a L<Map|/type/Map> of enum values. Works both on the enum type
95+
and any key.
96+
97+
enum Mass ( mg => 1/1000, g => 1/1, kg => 1000/1 );
98+
say Mass.enums; # OUTPUT: «Map.new((g => 1, kg => 1000, mg => 0.001))␤»
99+
say g.enums; # OUTPUT: «Map.new((g => 1, kg => 1000, mg => 0.001))␤»
100+
101+
=head1 Methods that work on the enum keys
77102

78103
=head2 method key
79104

@@ -82,7 +107,6 @@ An C<Enumeration> property.
82107
enum Norse-gods <Þor Oðin Freija>;
83108
say Freija.key; # OUTPUT: «Freija␤»
84109

85-
86110
=head2 method value
87111

88112
These are C<Enumeration> properties.
@@ -93,17 +117,6 @@ These are C<Enumeration> properties.
93117
The C<value> is assigned automatically by the C<enum> type starting at 0.
94118
C<Oðin> gets 1 since it is the second in the C<enum>.
95119

96-
=head2 method enums
97-
98-
method enums()
99-
100-
Returns a L<Map|/type/Map> of enum values. Works both on the enum type
101-
and any key.
102-
103-
enum Mass ( mg => 1/1000, g => 1/1, kg => 1000/1 );
104-
say Mass.enums; # OUTPUT: «Map.new((g => 1, kg => 1000, mg => 0.001))␤»
105-
say g.enums; # OUTPUT: «Map.new((g => 1, kg => 1000, mg => 0.001))␤»
106-
107120
=head2 method kv
108121

109122
multi method kv(::?CLASS:D:)

0 commit comments

Comments
 (0)