@@ -122,6 +122,17 @@ Returns it as a C<Pair>.
122
122
= for code :preamble<<enum Mass<g> >>
123
123
say g.pair; # OUTPUT: «g => 1»
124
124
125
+ = head2 method CALL-ME
126
+
127
+ Defined as:
128
+
129
+ multi method CALL-ME(|)
130
+
131
+ Returns an C < Enumeration > instance given an enum value.
132
+
133
+ enum Mass ( mg => 1/1000, g => 1/1, kg => 1000/1 );
134
+ say Mass(1/1000); # OUTPUT: mg
135
+
125
136
= head2 method pick
126
137
127
138
Defined as:
@@ -132,7 +143,7 @@ Defined as:
132
143
133
144
It works on the defined class, selecting one element and eliminating it.
134
145
135
- = for code :preamble< enum Norse-gods <Þor Oðin Freija>;>
146
+ = for code :preamble« enum Norse-gods <Þor Oðin Freija>;»
136
147
say Norse-gods.pick() for ^3; # OUTPUT: «ÞorFreijaOðin»
137
148
138
149
= head2 method roll
@@ -146,7 +157,7 @@ Defined as:
146
157
They work on the defined class selecting one or C < n > elements without
147
158
eliminating them.
148
159
149
- = for code :preamble< enum Norse-gods <Þor Oðin Freija>;>
160
+ = for code :preamble« enum Norse-gods <Þor Oðin Freija>;»
150
161
say Norse-gods.roll() for ^3; # OUTPUT: «FreijaFreijaOðin»
151
162
152
163
= head2 method pred
@@ -155,7 +166,7 @@ Defined as:
155
166
156
167
method pred(::?CLASS:D:)
157
168
158
- = for code :preamble< enum Norse-gods <Þor Oðin Freija>;>
169
+ = for code :preamble« enum Norse-gods <Þor Oðin Freija>;»
159
170
say Freija.pred; # OUTPUT: «Oðin»
160
171
161
172
= head2 method succ
@@ -164,9 +175,25 @@ Defined as:
164
175
165
176
method succ(::?CLASS:D:)
166
177
167
- = for code :preamble< enum Norse-gods <Þor Oðin Freija>;>
178
+ = for code :preamble« enum Norse-gods <Þor Oðin Freija>;»
168
179
say Oðin.succ; # OUTPUT: «Freija»
169
180
181
+ = head2 method Numeric
182
+
183
+ Defined as:
184
+
185
+ multi method Numeric(::?CLASS:D:)
186
+
187
+ Takes a value of an enum and returns it after coercion to C < Numeric > :
188
+
189
+ enum Numbers ( cool => '42', almost-pi => '3.14', sqrt-n-one => 'i' );
190
+ say cool.Numeric; # OUTPUT: «42»
191
+ say almost-pi.Numeric; # OUTPUT: «3.14»
192
+ say sqrt-n-one.Numeric; # OUTPUT: «0+1i»
193
+
194
+ Note that if the value cannot be coerced to C < Numeric > , an exception
195
+ will be thrown.
196
+
170
197
= head2 method Int
171
198
172
199
Defined as:
@@ -175,16 +202,30 @@ Defined as:
175
202
176
203
Takes a value of an enum and returns it after coercion to C < Int > :
177
204
178
- enum Numbers <One Two Three>;
179
- say Two.Int; # OUTPUT: «1»
205
+ enum Numbers ( cool => '42', almost-pi => '3.14', sqrt-n-one => 'i' );
206
+ say cool.Int; # OUTPUT: «42»
207
+ say almost-pi.Int; # OUTPUT: «3»
208
+ try say sqrt-n-one.Int;
209
+ say $!.message if $!; # OUTPUT: «Cannot convert 0+1i to Int: imaginary part not zero»
180
210
181
- Or
211
+ Note that if the value cannot be coerced to C < Int > , an exception will
212
+ be thrown.
182
213
183
- enum Numbers ( cool => '42', almost-pi => '3' );
184
- say cool.Int; # OUTPUT: «42»
185
- say almost-pi.Int; # OUTPUT: «3»
214
+ = head2 method Real
186
215
187
- Note that if the value cannot be coerced to C < Int > , an exception will
216
+ Defined as:
217
+
218
+ multi method Real(::?CLASS:D:)
219
+
220
+ Takes a value of an enum and returns it after coercion to C < Real > :
221
+
222
+ enum Numbers ( cool => '42', almost-pi => '3.14', sqrt-n-one => 'i' );
223
+ say cool.Real; # OUTPUT: «42»
224
+ say almost-pi.Real; # OUTPUT: «3.14»
225
+ try say sqrt-n-one.Real;
226
+ say $!.message if $!; # OUTPUT: «Cannot convert 0+1i to Real: imaginary part not zero»
227
+
228
+ Note that if the value cannot be coerced to C < Real > , an exception will
188
229
be thrown.
189
230
190
231
= head2 method C < === >
@@ -195,7 +236,7 @@ Defined as
195
236
196
237
Equality of C < Enumeration > symbols:
197
238
198
- = for code :preamble< enum Norse-gods <Þor Oðin Freija>;>
239
+ = for code :preamble« enum Norse-gods <Þor Oðin Freija>;»
199
240
say Norse-gods.pick() === Freija for ^3; # OUTPUT: «FalseFalseTrue»
200
241
201
242
0 commit comments