@@ -101,8 +101,8 @@ X<|classes, encapsulation>
101
101
102
102
The first three lines inside the class block all declare attributes (called
103
103
I < fields > or I < instance storage > in other languages). These are storage
104
- locations that every instance of a class will obtain. Just as a C < my > variable can
105
- not be accessed from the outside of its declared scope, attributes are not
104
+ locations that every instance of a class will obtain. Just as a C < my > variable
105
+ cannot be accessed from outside its declared scope, attributes are not
106
106
accessible outside of the class. This I < encapsulation > is one of the key
107
107
principles of object oriented design.
108
108
@@ -154,8 +154,8 @@ having to write the method by hand. You are free instead to write your own
154
154
accessor method, if at some future point you need to do something more complex
155
155
than return the value.
156
156
157
- Note that using the C < . > twigil has created a method that will provide with
158
- readonly access to the attribute. If instead the users of this object should be
157
+ Note that using the C < . > twigil has created a method that will provide
158
+ read-only access to the attribute. If instead the users of this object should be
159
159
able to reset a task's completion state (perhaps to perform it again), you can
160
160
change the attribute declaration:
161
161
@@ -168,8 +168,8 @@ external code can modify to change the value of the attribute.
168
168
169
169
= head1 Static fields?
170
170
171
- Perl 6 has no B < static > keyword. Anyway any class may declare anything that
172
- module can so making a scoped variable sounds like good idea.
171
+ Perl 6 has no B < static > keyword. Nevertheless any class may declare anything
172
+ that a module can, so making a scoped variable sounds like good idea.
173
173
174
174
= begin code
175
175
@@ -184,31 +184,30 @@ module can so making a scoped variable sounds like good idea.
184
184
185
185
= end code
186
186
187
- Class attributes defined by L < my > or L < our > may also be initialized while
188
- declaration but we are implementing Singleton pattern here and object must
189
- be created during first use. It is not possible to predict in 100% the moment
190
- when attribute initialization will be executed because it can be done during
191
- compilation, runtime or both especially when importing class using L < use >
192
- keyword.
187
+ Class attributes defined by L < my > or L < our > may also be initialized when
188
+ being declared, however we are implementing the Singleton pattern here and
189
+ the object must be created during its first use. It is not 100% possible to
190
+ predict the moment when attribute initialization will be executed, because
191
+ it can take place during compilation, runtime or both, especially when
192
+ importing the class using the L < use > keyword.
193
193
194
194
class HaveStaticAttr {
195
195
my Foo $.foo = some_complicated_subroutine;
196
196
}
197
197
198
- Class attributes may also be declared with secondary sigil like object
199
- attributes that will result in generating read-only accessor if attribute
200
- is to be public.
201
-
198
+ Class attributes may also be declared with a secondary sigil -- in a similar
199
+ manner to object attributes -- that will generate read-only accessors if the
200
+ attribute is to be public.
202
201
203
202
= head1 Methods
204
203
205
204
X < |methods >
206
205
X < |classes, methods >
207
206
208
- While attributes give objects state, methods give objects behaviors. Ignore
209
- the C < new > method temporarily; it's a special type of method. Consider the
210
- second method, C < add-dependency > , which adds a new task to this task's
211
- dependency list.
207
+ While attributes give objects state, methods give objects behaviors. Let's
208
+ ignore the C < new > method temporarily; it's a special type of method.
209
+ Consider the second method, C < add-dependency > , which adds a new task to a
210
+ task's dependency list.
212
211
213
212
method add-dependency(Task $dependency) {
214
213
push @!dependencies, $dependency;
@@ -222,8 +221,8 @@ the list of methods for the current class. Thus any instance of the C<Task>
222
221
class can call this method with the C < . > method call operator. Second, a
223
222
method places its invocant into the special variable C < self > .
224
223
225
- The method itself takes the passed parameter-- which must be an instance of the
226
- C < Task > class-- and C < push > es it onto the invocant's C < @!dependencies >
224
+ The method itself takes the passed parameter -- which must be an instance of
225
+ the C < Task > class -- and C < push > es it onto the invocant's C < @!dependencies >
227
226
attribute.
228
227
229
228
The second method contains the main logic of the dependency handler:
@@ -244,7 +243,7 @@ X<|operators, .>
244
243
245
244
Otherwise, the method performs all of the task's dependencies, using the C < for >
246
245
construct to iterate over all of the items in the C < @!dependencies > attribute.
247
- This iteration places each item-- each a C < Task > object-- into the topic
246
+ This iteration places each item -- each a C < Task > object -- into the topic
248
247
variable, C < $_ > . Using the C < . > method call operator without specifying an
249
248
explicit invocant uses the current topic as the invocant. Thus the iteration
250
249
construct calls the C < .perform() > method on every C < Task > object in the
@@ -275,19 +274,19 @@ X<|objects, bless>
275
274
X < |bless >
276
275
277
276
The biggest difference between constructors in Perl 6 and constructors in
278
- languages such as C# and Java is that rather than setting up state on a somehow
279
- already magically created object, Perl 6 constructors actually create the
280
- object themselves. This easiest way to do this is by calling the L < bless >
281
- method, also inherited from L < Mu > . The C < bless > method expects a set of named parameters providing
282
- the initial values for each attribute.
277
+ languages such as C# and Java is that rather than setting up state on a
278
+ somehow already magically created object, Perl 6 constructors actually
279
+ create the object themselves. The easiest way to do this is by calling the
280
+ L < bless > method, also inherited from L < Mu > . The C < bless > method expects a
281
+ set of named parameters providing the initial values for each attribute.
283
282
284
283
The example's constructor turns positional arguments into named arguments, so
285
284
that the class can provide a nice constructor for its users. The first
286
- parameter is the callback (the thing to do to execute the task). The rest of
285
+ parameter is the callback (the thing which will execute the task). The rest of
287
286
the parameters are dependent C < Task > instances. The constructor captures these
288
287
into the C < @dependencies > slurpy array and passes them as named parameters to
289
- C < bless > (note that C < :&callback > uses the name of the variable-- minus the
290
- sigil-- as the name of the parameter).
288
+ C < bless > (note that C < :&callback > uses the name of the variable -- minus the
289
+ sigil -- as the name of the parameter).
291
290
292
291
X < |BUILD >
293
292
@@ -350,7 +349,7 @@ the various other dependencies in order, giving the output:
350
349
Object Oriented Programming provides the concept of inheritance as one of the
351
350
mechanisms to allow for code reuse. Perl 6 supports the ability for one class
352
351
to inherit from one or more classes. When a class inherits from another class
353
- that informs the method dispatcher to follow the inheritance chain to look
352
+ it informs the method dispatcher to follow the inheritance chain to look
354
353
for a method to dispatch. This happens both for standard methods defined via
355
354
the method keyword and for methods generated through other means such as
356
355
attribute accessors.
@@ -365,7 +364,6 @@ improved). See L<https://github.com/perl6/book/issues/58> for discussion.
365
364
method pay() {
366
365
say "Here is \$$.salary";
367
366
}
368
-
369
367
}
370
368
371
369
class Programmer is Employee {
@@ -393,7 +391,7 @@ defined in the Employee class as though they were from the Programmer class.
393
391
$programmer.pay();
394
392
= end code
395
393
396
- = head2 Overriding Inherited Methods
394
+ = head2 Overriding inherited methods
397
395
398
396
Of course, classes can override methods and attributes defined by parent
399
397
classes by defining their own. The example below demonstrates the C < Baker > class
@@ -437,18 +435,20 @@ overriding the C<Cook>'s C<cook> method.
437
435
Because the dispatcher will see the C < cook > method on C < Baker > before it moves up to
438
436
the parent class the C < Baker > 's C < cook > method will be called.
439
437
440
- As a side node , you might have wondered how passing strings to array attribute
441
- worked. Array assignment is coercive, so the assignment turn the new value
438
+ As a side note , you might have wondered how passing strings to an array attribute
439
+ worked. Array assignment is coercive, so the assignment turns the new value
442
440
into an array. This mechanism is also used for initializing attributes.
443
441
444
- = head2 Multiple Inheritance
442
+ = head2 Multiple inheritance
445
443
446
444
As mentioned before, a class can inherit from multiple classes. When a class
447
445
inherits from multiple classes the dispatcher knows to look at both classes when
448
- looking up a method to search for. As a side note, Perl 6 uses the C3
449
- algorithm to linearize the multiple inheritance hierarchies, which is a
446
+ looking up a method to search for. Perl 6 uses the C3
447
+ algorithm to linearize multiple inheritance hierarchies, which is a
450
448
significant improvement over Perl 5's approach to handling multiple inheritance.
451
449
450
+ = comment isn't Perl 5 also C3 now?
451
+
452
452
= begin code
453
453
class GeekCook is Programmer is Cook {
454
454
method new( *%params ) {
@@ -468,10 +468,10 @@ significant improvement over Perl 5's approach to handling multiple inheritance.
468
468
$geek.code_to_solve('P =? NP');
469
469
= end code
470
470
471
- Now all the methods made available by both the Programmer class and the Cook
472
- class are available from the GeekCook class.
471
+ Now all the methods made available to the Programmer and the Cook
472
+ classes are available from the GeekCook class.
473
473
474
- While multiple inheritance is a useful concept to know and on occasion of use, it
474
+ While multiple inheritance is a useful concept to know and occasionally use, it
475
475
is important to understand that there are more useful OOP concepts. When
476
476
reaching for multiple inheritance it is good practice to consider whether the
477
477
design wouldn't be better realized by using roles, which are generally safer
@@ -482,9 +482,9 @@ names. For more information on roles see A<sec:roles>.
482
482
483
483
Introspection is the process of gathering information about some objects in
484
484
your program, not by reading the source code, but by querying the object (or a
485
- controlling object) for some properties, like its type.
485
+ controlling object) for some properties, such as its type.
486
486
487
- Given an object C < $p > , and the class definitions from the previous sections,
487
+ Given an object C < $o > , and the class definitions from the previous sections,
488
488
we can ask it a few questions:
489
489
490
490
if $o ~~ Employee { say "It's an employee" };
@@ -496,7 +496,6 @@ we can ask it a few questions:
496
496
497
497
The output can look like this:
498
498
499
- = for output
500
499
It's an employee
501
500
(Programmer)
502
501
Programmer.new(known_languages => ["Perl", "Python", "Pascal"],
@@ -510,7 +509,7 @@ question is of class C<Employee> or one that inherits from it, but not
510
509
C < GeekCook > .
511
510
512
511
The C < .WHAT > method returns the type object associated with the object C < $o > ,
513
- which tells the exact type of C < $o > : in this case C < Programmer > .
512
+ which tells us the exact type of C < $o > : in this case C < Programmer > .
514
513
515
514
C < $o.perl > returns a string that can be executed as Perl code, and reproduces
516
515
the original object C < $o > . While this does not work perfectly in all
@@ -522,11 +521,11 @@ is very useful for debugging simple objects.
522
521
523
522
C < $o.^methods(:local) > produces a list of methods that can be called
524
523
on C < $o > . The C < :local > named argument limits the returned methods to those
525
- defined in the C < Employee > class, and excludes the inherited methods.
524
+ defined in the C < Employee > class and excludes the inherited methods.
526
525
527
- The syntax of calling method with C < .^ > instead of a single dot means that it
528
- is actually a method call on the I < meta class > , which is a class managing the
529
- properties of the C < Employee > class - or any other class you are interested
526
+ The syntax of calling a method with C < .^ > instead of a single dot means that it
527
+ is actually a method call on the I < meta class > , which is a class managing the
528
+ properties of the C < Employee > class -- or any other class you are interested
530
529
in. This meta class enables other ways of introspection too:
531
530
532
531
say $o.^attributes.join(', ');
@@ -537,9 +536,9 @@ unsurprisingly returns the class name.
537
536
538
537
Introspection is very useful for debugging, and for learning the language
539
538
and new libraries. When a function or method
540
- returns an object you don't know about, finding its type with C < .WHAT > , a
541
- construction recipe for it with C < .perl > and so on you'll get a good idea what
542
- this return value is. With C < .^methods > you can learn what you can do with it .
539
+ returns an object you don't know about, finding its type with C < .WHAT > , seeing a
540
+ construction recipe for it with C < .perl > , and so on, you'll get a good idea of what
541
+ its return value is. With C < .^methods > you can learn what you can do with the class .
543
542
544
543
But there are other applications too: a routine that serializes objects to a
545
544
bunch of bytes needs to know the attributes of that object, which it can find
0 commit comments