@@ -112,7 +112,7 @@ sub f($x) {
112
112
...
113
113
}
114
114
115
- Subtype checking is done by X < smart-matching|smartmatch operator > :
115
+ Subtype checking is done by L < smart-matching|smart-match operator > :
116
116
117
117
= for code :preamble<my $type;>
118
118
if $type ~~ Real {
@@ -124,15 +124,13 @@ if $type ~~ Real {
124
124
Classes are declared using the C < class > keyword, typically followed by a
125
125
name.
126
126
127
- class Journey {
128
- }
127
+ class Journey { }
129
128
130
129
This declaration results in a type object being created and installed in the
131
130
current package and current lexical scope under the name C < Journey > . You
132
131
can also declare classes lexically:
133
132
134
- my class Journey {
135
- }
133
+ my class Journey { }
136
134
137
135
This restricts their visibility to the current lexical scope, which can be
138
136
useful if the class is an implementation detail nested inside a module or
@@ -179,8 +177,9 @@ and C<.notes> will all be accessible from outside the class, but only
179
177
C < .notes > can be modified.
180
178
181
179
If an object is instantiated without certain attributes, such as origin or
182
- destination, we may not get the desired result . To prevent this, provide default values or make
183
- sure that an attribute is set on object creation by marking an attribute with an L < is required > trait.
180
+ destination, we may not get the desired result. To prevent this, provide
181
+ default values or make sure that an attribute is set on object creation
182
+ by marking an attribute with an L < is required > trait.
184
183
185
184
class Journey {
186
185
# error if origin is not provided
@@ -195,10 +194,10 @@ Since classes inherit a default constructor from C<Mu> and we have requested
195
194
that some accessor methods are generated for us, our class is already
196
195
somewhat functional.
197
196
198
- = begin code :allow<B L> : preamble<class Journey {};>
197
+ = begin code :preamble<class Journey {};>
199
198
# Create a new instance of the class.
200
199
my $vacation = Journey.new(
201
- origin L « => » 'Sweden',
200
+ origin => 'Sweden',
202
201
destination => 'Switzerland',
203
202
notes => 'Pack hiking gear!'
204
203
);
@@ -210,30 +209,32 @@ somewhat functional.
210
209
$vacation.notes = 'Pack hiking gear and sunglasses!';
211
210
= end code
212
211
213
- Note that the default constructor will only set attributes that have an
214
- accessor method, but it can initialize read-only attributes.
212
+ Note that, although the default constructor can initialize read-only
213
+ attributes, it will only set attributes that have an accessor method.
214
+ That is, even if you pass C « travelers => ["Alex", "Betty"] » to the
215
+ default constructor, the attribute C < @!travelers > is not initialized.
215
216
216
217
= head2 Methods
217
218
218
219
Methods are declared with the C < method > keyword inside a class body.
219
220
220
- = begin code :allow<B L>
221
+ = begin code
221
222
class Journey {
222
223
has $.origin;
223
224
has $.destination;
224
225
has @!travelers;
225
226
has $.notes is rw;
226
227
227
- B < method > add_traveler ($name) {
228
- if $name L < ne > L < any > (@!travelers) {
229
- L < push > @!travelers, $name;
228
+ method add-traveler ($name) {
229
+ if $name ne any(@!travelers) {
230
+ push @!travelers, $name;
230
231
}
231
232
else {
232
- L < warn > "$name is already going on the journey!";
233
+ warn "$name is already going on the journey!";
233
234
}
234
235
}
235
236
236
- B < method > describe() {
237
+ method describe() {
237
238
"From $!origin to $!destination"
238
239
}
239
240
}
@@ -246,11 +247,10 @@ declares a C<!> twigil and generates an accessor method.
246
247
247
248
Looking at the code above,
248
249
there is a subtle but important difference between using C < $!origin >
249
- and C < $.origin > in the method C < describe > . The first is always a lookup of the
250
- attribute. The first is an inexpensive and obvious lookup of the attribute.
251
- C < $.origin > is a method call and thus may
252
- be overridden in a subclass. Only use C < $.origin > if you want to
253
- allow overriding.
250
+ and C < $.origin > in the method C < describe > .
251
+ C < $!origin > is an inexpensive and obvious lookup of the attribute.
252
+ C < $.origin > is a method call and thus may be overridden in a subclass.
253
+ Only use C < $.origin > if you want to allow overriding.
254
254
255
255
Unlike Subroutines, additional named arguments will not produce compile time or
256
256
runtime errors. That allows chaining of methods via
0 commit comments