@@ -14,7 +14,7 @@ Native types are used for low-level types (like C<uint64>). They do not
14
14
have the same capabilities as objects, though if you call methods on them,
15
15
they are I < boxed > into normal objects.
16
16
17
- Everything that you can store in a variable is either a native value, or an
17
+ Everything that you can store in a variable is either a native value or an
18
18
object. That includes literals, types (type objects), code and containers.
19
19
20
20
= head1 Using Objects
@@ -36,7 +36,7 @@ my $formatted-text = "Fourscore and seven years ago...".L<indent>B<(8)>;
36
36
37
37
C < $formatted-text > now contains the above text, but indented 8 spaces.
38
38
39
- Multiple arguments just require a comma between them:
39
+ Multiple arguments simply require a comma between them:
40
40
41
41
= for code :allow<B L>
42
42
my @words = "Abe", "Lincoln";
@@ -50,7 +50,7 @@ say @words.L<join>: '--';
50
50
# Abe--Lincoln--said--Fourscore--and--seven--years--ago
51
51
52
52
Since you have to put a C < : > after the method if you want to pass arguments
53
- without parenthesis , a method call without a colon or parens is unambiguously
53
+ without parentheses , a method call without a colon or parens is unambiguously
54
54
a method call without an argument list:
55
55
56
56
$computation-result.say;
@@ -75,7 +75,7 @@ of the type hierarchy. In other words, all objects are derived from C<Mu>.
75
75
76
76
= head2 Type Objects
77
77
78
- Types themselves are objects, and you can get the I < type object > simply
78
+ Types themselves are objects and you can get the I < type object > simply
79
79
by writing its name:
80
80
81
81
my $int-type-obj = Int;
@@ -149,7 +149,7 @@ are typically declared using the C<has> declarator and using the C<!> twigil.
149
149
150
150
While there is no such thing as a public (or even protected) attribute, there
151
151
is a way to have accessor methods generated automatically: replace the C < ! >
152
- twigil with the C < . > twigil (the C < . > should remind you of method call).
152
+ twigil with the C < . > twigil (the C < . > should remind you of a method call).
153
153
154
154
class Journey {
155
155
has $.origin;
@@ -169,7 +169,7 @@ the attribute, add the C<rw> trait:
169
169
}
170
170
171
171
Now, after a C < Journey > object is created, its C < .origin > ,
172
- C < .destination > , and C < .notes > will all be accessible from outside of the
172
+ C < .destination > , and C < .notes > will all be accessible from outside the
173
173
class, but only C < .notes > can be modified.
174
174
175
175
Since classes inherit a default constructor from C < Mu > and we have requested
@@ -196,7 +196,7 @@ accessor method.
196
196
197
197
= head2 Methods
198
198
199
- Methods are declared with the C < method > keyword inside of a class body.
199
+ Methods are declared with the C < method > keyword inside a class body.
200
200
201
201
= begin code :allow<B L>
202
202
class Journey {
@@ -221,29 +221,29 @@ Methods are declared with the C<method> keyword inside of a class body.
221
221
= end code
222
222
223
223
A method can have a signature, just like a subroutine. Attributes can be used
224
- in methods, and can always be used with the C < ! > twigil, even if they are
224
+ in methods and can always be used with the C < ! > twigil, even if they are
225
225
declared with the C < . > twigil. This is because really, the C < . > twigil declares
226
- an attribute with the C < ! > twigil in its place, and then additionally generates
226
+ an attribute with the C < ! > twigil in its place and then additionally generates
227
227
an accessor method.
228
228
229
229
There is a subtle but important difference between using, say, C < $!origin > and
230
230
C < $.origin > in the method C < describe > . The first is always a simple lookup of
231
231
the attribute. It is cheap, and you know that it is the attribute declared in
232
- this class. The latter is really a method call, and thus it may be overridden
232
+ this class. The latter is really a method call and thus it may be overridden
233
233
in a subclass. Only use C < $.origin > if you explicitly want to allow overriding.
234
234
235
235
= head2 self
236
236
237
- Inside a method, the term C < self > is available, which is bound to invocant, i.e.
237
+ Inside a method, the term C < self > is available, which is bound to the invocant, i.e.
238
238
the object that the method was called on. C < self > can be used to call further
239
- methods on the invocant, for example . Within methods, something like
239
+ methods on the invocant. Within methods, something like
240
240
C < $.origin > is the same thing as C < self.origin > .
241
241
242
242
= head2 Private Methods
243
243
244
- Methods for internal usage inside the class that cannot be called from
245
- anywhere else are introduced with an exclamation mark C < ! > before the method
246
- name, and are called with the exclamation mark instead of the dot:
244
+ Methods strictly for use inside the class and that are not to be called from
245
+ anywhere else are declared with an exclamation mark C < ! > before the method
246
+ name. They are called with an exclamation mark instead of a dot:
247
247
248
248
method !do-something-private($x) {
249
249
...
@@ -258,7 +258,7 @@ Private methods are not inherited to subclasses.
258
258
259
259
= head2 Submethods
260
260
261
- A submethod is public method that is not inherited to subclasses. The name
261
+ A submethod is a public method that is not inherited to subclasses. The name
262
262
stems from the fact that they are semantically similar to subroutines.
263
263
264
264
Submethods are useful for object construction and destruction tasks, as well
@@ -275,12 +275,11 @@ Classes can have I<parent classes>.
275
275
class Child B < L < is > Parent1 is Parent2> { }
276
276
277
277
If a method is called on the child class, and the child class does not
278
- provide that method, the parent classes' method of that name is invoked instead,
278
+ provide that method, the method of that name in one of the parent classes is invoked instead,
279
279
if it exists. The order in which parent classes are consulted is called the
280
280
I < method resolution order > (MRO). Perl 6 uses the
281
281
L < C3 method resolution order|https://en.wikipedia.org/wiki/C3_linearization > .
282
- You can ask a type for its MRO through a call to its metaclass:
283
-
282
+ You can ask a type for its MRO through a call to its meta class:
284
283
285
284
= for code :allow<B L>
286
285
say ParcelB < .^L < mro > > ; # Parcel() Cool() Any() Mu()
@@ -289,7 +288,7 @@ If a class does not specify a parent class, L<Any> is assumed by default. All
289
288
classes directly or indirectly derive from L < Mu > , the root of the type
290
289
hierarchy.
291
290
292
- All calls to public method are "virtual" in the C++ sense, which means that
291
+ All calls to public methods are "virtual" in the C++ sense, which means that
293
292
the actual type of an object determines which method to call, not the declared
294
293
type:
295
294
@@ -317,7 +316,7 @@ This produces the output:
317
316
318
317
= head2 Object Construction
319
318
320
- Objects are generally created through methods calls, either on the type object
319
+ Objects are generally created through method calls, either on the type object
321
320
or on another object of the same type.
322
321
323
322
Class L < Mu > provides a constructor method called L < new > , which takes named
@@ -350,10 +349,10 @@ This outputs:
350
349
x: 5
351
350
y: 10
352
351
353
- Mu.new calls method L < bless > on its invocant, passing along all the named
354
- arguments. C < bless > creates the new object, and then calls method C < BUILDALL >
352
+ C < Mu.new > calls method L < bless > on its invocant, passing all the named
353
+ arguments. C < bless > creates the new object and then calls method C < BUILDALL >
355
354
on it. C < BUILDALL > walks all subclasses in reverse method resolution order
356
- (i.e. from L < Mu > to most derived classes), and in each class checks for
355
+ (i.e. from L < Mu > to most derived classes) and in each class checks for
357
356
existence of a method named C < BUILD > . If it exists, it is called, again
358
357
passing all named arguments from method C < new > to it. If not, the public
359
358
attributes from this class are initialized from named arguments of the same
@@ -362,7 +361,7 @@ initialized the attribute, default values are applied (the C<2 * $!x> in the
362
361
example above).
363
362
364
363
Due to the nature of the default behavior of C < BUILDALL > and custom C < BUILD >
365
- submethods, you named arguments to the constructor C < new > derived from C < Mu >
364
+ submethods, named arguments to the constructor C < new > derived from C < Mu >
366
365
can correspond directly to public attributes of any of the classes in the
367
366
method resolution order, or to any named parameter to any C < BUILD > submethod.
368
367
@@ -388,8 +387,8 @@ time. They can also be used for creating aliases for attribute initialization:
388
387
= end code
389
388
390
389
Since passing arguments to a routine binds the arguments to the parameters,
391
- a separate binding step is unnecessary if the attribute is used as parameter.
392
- So the example above could also have been written as:
390
+ a separate binding step is unnecessary if the attribute is used as a parameter.
391
+ Hence the example above could also have been written as:
393
392
394
393
= for code :allow<B L>
395
394
submethod BUILD(:encoding(:$B < ! > enc), :$B < ! > data) {
@@ -420,9 +419,9 @@ use C<CREATE> to fiddle around with low-level workings.
420
419
421
420
Roles are in some ways similar to classes, in that they are a collection of
422
421
attributes and methods. They differ in that roles are also meant for
423
- describing only parts of an object's behavior, and in how roles are applied to
422
+ describing only parts of an object's behavior and in how roles are applied to
424
423
classes. Or to phrase it differently, classes are meant for managing
425
- instances, and roles are meant for managing behavior and code reuse.
424
+ instances and roles are meant for managing behavior and code reuse.
426
425
427
426
= begin code :allow<B L>
428
427
role Serializable {
@@ -482,8 +481,8 @@ and a class C<Automobile>, for things that you can drive.
482
481
my $t = Taurus.new;
483
482
$t.steer; # Castrates $t
484
483
485
- With this set-up , your poor customers will find themselves unable to
486
- turn their Taurus, and you won't be able to make more of your product!
484
+ With this setup , your poor customers will find themselves unable to
485
+ turn their Taurus and you won't be able to make more of your product!
487
486
In this case, it may have been better to use roles:
488
487
489
488
role Bull-Like {
@@ -508,7 +507,7 @@ This code will die with something like:
508
507
Method 'steer' must be resolved by class Taurus because it exists in
509
508
multiple roles (Steerable, Bull-Like)
510
509
511
- This check will save you and your customers a lot of headaches, and you
510
+ This check will save you and your customers a lot of headaches and you
512
511
can simply define your class instead as:
513
512
514
513
class Taurus does Bull-Like does Steerable {
@@ -548,7 +547,7 @@ This allows you to create roles that act as abstract interfaces.
548
547
= begin code :allow<B L>
549
548
role AbstractSerializable {
550
549
method serialize() { B < L < ... > > } # literal ... here marks the
551
- # method as a stub
550
+ # method as a stub
552
551
}
553
552
554
553
# the following is a compile time error, for example
@@ -580,7 +579,7 @@ objects; those objects are called I<meta objects>. Meta objects are, like
580
579
ordinary objects, instances of classes, in this case we call them I < meta
581
580
classes > .
582
581
583
- For each object or class, you can get the meta object by calling C < .HOW > on
582
+ For each object or class you can get the meta object by calling C < .HOW > on
584
583
it. Note that although this looks like a method call, it is actually
585
584
special-cased in the compiler, so it is more like a macro.
586
585
@@ -599,7 +598,7 @@ C<Perl6::Metamodel::ClassHOW>.
599
598
600
599
But of course the meta model does much more for you. For example it allows you
601
600
to introspect objects and classes. The calling convention for methods on meta
602
- objects is to call the method on the meta object, and pass in the object of
601
+ objects is to call the method on the meta object and pass in the object of
603
602
interest as first argument to the object. So to get the name of the class of
604
603
an object, you could write:
605
604
@@ -621,7 +620,7 @@ To get rid of using the same object twice, there is a shortcut:
621
620
say 1.HOW.name(1); # Int
622
621
623
622
See L < Metamodel::ClassHOW|/type/Metamodel::ClassHOW > for documentation of
624
- the meta class of C < class > , and also the L < general documentation on the meta
623
+ the meta class of C < class > and also the L < general documentation on the meta
625
624
object protocol|/language/mop > .
626
625
627
626
= end pod
0 commit comments