@@ -71,9 +71,34 @@ enum Foo <bar baz>;
71
71
say baz ~~ Foo; # OUTPUT: «True»
72
72
say Foo ~~ bar; # OUTPUT: «False»
73
73
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.
75
86
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
77
102
78
103
=head2 method key
79
104
@@ -82,7 +107,6 @@ An C<Enumeration> property.
82
107
enum Norse-gods <Þor Oðin Freija>;
83
108
say Freija.key; # OUTPUT: «Freija»
84
109
85
-
86
110
=head2 method value
87
111
88
112
These are C<Enumeration> properties.
@@ -93,17 +117,6 @@ These are C<Enumeration> properties.
93
117
The C<value> is assigned automatically by the C<enum> type starting at 0.
94
118
C<Oðin> gets 1 since it is the second in the C<enum>.
95
119
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
-
107
120
=head2 method kv
108
121
109
122
multi method kv(::?CLASS:D:)
0 commit comments