Skip to content

Commit aa93bc2

Browse files
authored
unified format
and add note about default constructor
1 parent 91eb48a commit aa93bc2

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

doc/Language/objects.pod6

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ sub f($x) {
112112
...
113113
}
114114
115-
Subtype checking is done by X<smart-matching|smartmatch operator>:
115+
Subtype checking is done by L<smart-matching|smart-match operator>:
116116
117117
=for code :preamble<my $type;>
118118
if $type ~~ Real {
@@ -124,15 +124,13 @@ if $type ~~ Real {
124124
Classes are declared using the C<class> keyword, typically followed by a
125125
name.
126126
127-
class Journey {
128-
}
127+
class Journey { }
129128
130129
This declaration results in a type object being created and installed in the
131130
current package and current lexical scope under the name C<Journey>. You
132131
can also declare classes lexically:
133132
134-
my class Journey {
135-
}
133+
my class Journey { }
136134
137135
This restricts their visibility to the current lexical scope, which can be
138136
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
179177
C<.notes> can be modified.
180178
181179
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.
184183
185184
class Journey {
186185
# error if origin is not provided
@@ -195,10 +194,10 @@ Since classes inherit a default constructor from C<Mu> and we have requested
195194
that some accessor methods are generated for us, our class is already
196195
somewhat functional.
197196
198-
=begin code :allow<B L> :preamble<class Journey {};>
197+
=begin code :preamble<class Journey {};>
199198
# Create a new instance of the class.
200199
my $vacation = Journey.new(
201-
origin L«=>» 'Sweden',
200+
origin => 'Sweden',
202201
destination => 'Switzerland',
203202
notes => 'Pack hiking gear!'
204203
);
@@ -210,30 +209,32 @@ somewhat functional.
210209
$vacation.notes = 'Pack hiking gear and sunglasses!';
211210
=end code
212211
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.
215216
216217
=head2 Methods
217218
218219
Methods are declared with the C<method> keyword inside a class body.
219220
220-
=begin code :allow<B L>
221+
=begin code
221222
class Journey {
222223
has $.origin;
223224
has $.destination;
224225
has @!travelers;
225226
has $.notes is rw;
226227
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;
230231
}
231232
else {
232-
L<warn> "$name is already going on the journey!";
233+
warn "$name is already going on the journey!";
233234
}
234235
}
235236
236-
B<method> describe() {
237+
method describe() {
237238
"From $!origin to $!destination"
238239
}
239240
}
@@ -246,11 +247,10 @@ declares a C<!> twigil and generates an accessor method.
246247
247248
Looking at the code above,
248249
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.
254254
255255
Unlike Subroutines, additional named arguments will not produce compile time or
256256
runtime errors. That allows chaining of methods via

0 commit comments

Comments
 (0)