You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/Language/classtut.pod6
+31-19Lines changed: 31 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
=beginpod :tag<tutorial>
2
2
3
-
=TITLEClasses and Objects
3
+
=TITLEClasses and objects
4
4
5
5
=SUBTITLEA tutorial about creating and using classes in Perl 6
6
6
@@ -148,9 +148,9 @@ X<|type object>
148
148
X<|DEFINITE>
149
149
X<|.DEFINITE>
150
150
151
-
Declaring a class creates a new I<type object> which, by default, is installed into
152
-
the current package (just like a variable declared with C<our> scope). This
153
-
type object is an "empty instance" of the class. For example, types such as
151
+
Declaring a class creates a new I<type object> which, by default, is installed
152
+
into the current package (just like a variable declared with C<our> scope).
153
+
This type object is an "empty instance" of the class. For example, types such as
154
154
C<Int> and C<Str> refer to the type object of one of the Perl 6 built-in
155
155
classes. The example above uses the class name C<Task> so that other code can
156
156
refer to it later, such as to create class instances by calling the C<new>
@@ -261,7 +261,12 @@ has Task @!dependencies;
261
261
has $.ready = not @!dependencies;
262
262
263
263
X<|class variables>
264
-
A class declaration can also include I<class variables>, which are variables that are shared by all instances, and can be used for things like counting the number of instantiations or any other shared state. Class variables use the same syntax as the rest of the attributes, but are declared as C<my> or C<our>, depending on the scope; the second type of variables will be used across the class hierarchy.
264
+
A class declaration can also include I<class variables>, which are variables
265
+
that are shared by all instances, and can be used for things like counting the
266
+
number of instantiations or any other shared state. Class variables use the same
267
+
syntax as the rest of the attributes, but are declared as C<my> or C<our>,
268
+
depending on the scope; the second type of variables will be used across the
269
+
class hierarchy.
265
270
266
271
=begincode
267
272
class Str-with-ID is Str {
@@ -304,11 +309,11 @@ that a module can, so making a scoped variable sounds like good idea.
304
309
305
310
=endcode
306
311
307
-
Class attributes defined by L<my|/syntax/my> or L<our|/syntax/our> may also be initialized when
308
-
being declared, however we are implementing the Singleton pattern here and
309
-
the object must be created during its first use. It is not 100% possible to
310
-
predict the moment when attribute initialization will be executed, because
311
-
it can take place during compilation, runtime or both, especially when
312
+
Class attributes defined by L<my|/syntax/my> or L<our|/syntax/our> may also be
313
+
initialized when being declared, however we are implementing the Singleton
314
+
pattern here and the object must be created during its first use. It is not 100%
315
+
possible to predict the moment when attribute initialization will be executed,
316
+
because it can take place during compilation, runtime or both, especially when
312
317
importing the class using the L<use|/syntax/use> keyword.
313
318
314
319
=begincode :skip-test
@@ -365,7 +370,6 @@ it checks if the task has already completed by checking the C<$!done>
365
370
attribute. If so, there's nothing to do.
366
371
367
372
X<|operators,.>
368
-
369
373
Otherwise, the method performs all of the task's dependencies, using the
370
374
C<for> construct to iterate over all of the items in the C<@!dependencies>
371
375
attribute. This iteration places each item – each a C<Task> object – into
@@ -382,15 +386,15 @@ on this object (if this C<Task> is a dependency of another C<Task>, for
382
386
example) will not repeat the task.
383
387
384
388
X<|! (private methods)>
385
-
=head2Private Methods
389
+
=head2Private methods
386
390
387
391
Just like attributes, methods can also be private. Private methods are declared
388
392
with a prefixed exclamation mark. They are called with C<self!> followed by the
389
393
method's name. To call a private method of another class the calling class has
390
394
to be trusted by the called class. A trust relationship is declared with
391
395
C<trusts> and the class to be trusted must already be declared. Calling a
392
-
private method of another class requires an instance of that class and the
393
-
fully qualified name of the method. Trust also allows access to private attributes.
396
+
private method of another class requires an instance of that class and the fully
397
+
qualified name of the method. Trust also allows access to private attributes.
394
398
395
399
class B {...}
396
400
@@ -417,9 +421,8 @@ fully qualified name of the method. Trust also allows access to private attribu
417
421
Trust relationships are not subject to inheritance. To trust the global
418
422
namespace, the pseudo package C<GLOBAL> can be used.
419
423
420
-
=head1Constructors
421
-
422
424
X<|constructors>
425
+
=head1Constructors
423
426
424
427
Perl 6 is rather more liberal than many languages in the area of
425
428
constructors. A constructor is anything that returns an instance of the
@@ -753,7 +756,11 @@ find out via introspection.
753
756
754
757
=head2X<Overriding default gist method>
755
758
756
-
Some classes might need its own version of C<gist>, which overrides the terse way it is printed when called to provide a default representation of the class. For instance, L<exceptions|/language/exceptions#Uncaught_Exceptions> might want to write just the C<payload> and not the full object so that it is clearer what has happened. But you can do that with every class:
759
+
Some classes might need its own version of C<gist>, which overrides the terse
760
+
way it is printed when called to provide a default representation of the class.
761
+
For instance, L<exceptions|/language/exceptions#Uncaught_Exceptions> might want
762
+
to write just the C<payload> and not the full object so that it is clearer what
763
+
has happened. But you can do that with every class:
757
764
758
765
=begincode
759
766
class Cook {
@@ -769,7 +776,9 @@ class Cook {
769
776
}
770
777
771
778
multi method gist(Cook:U:) { '⚗' ~ self.^name ~ '⚗' }
772
-
multi method gist(Cook:D:) { '⚗ Cooks with ' ~ @.utensils.join( " ‣ ") ~ ' using ' ~ @.cookbooks.map( "«" ~ * ~ "»").join( " and ") }
779
+
multi method gist(Cook:D:) {
780
+
'⚗ Cooks with ' ~ @.utensils.join( " ‣ ") ~ ' using '
@@ -780,7 +789,10 @@ say Cook.gist; # OUTPUT: «⚗Cook⚗»
780
789
say $cook.gist; # OUTPUT: «⚗ Cooks with spoon ‣ ladle ‣ knife ‣ pan using «Cooking for geeks» and «The French Chef Cookbook»»
781
790
=endcode
782
791
783
-
Usually you will want to define two methods, one for the class and another for instances; in this case, the class method uses the alembic symbol, and the instance method, defined below, aggregates the data we have on the cook to show it in a narrative way.
792
+
Usually you will want to define two methods, one for the class and another for
793
+
class instances; in this case, the class method uses the alembic symbol, and the
794
+
instance method, defined below, aggregates the data we have on the cook to show
0 commit comments