Skip to content

Commit a314d6d

Browse files
committed
Adds clarification and examples for class and class hierarchy vars
This closes #2876 which, besides, did have a bug using attribute syntax for class variables, which does not make a lot of sense.
1 parent b7b9422 commit a314d6d

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

doc/Language/classtut.pod6

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -294,35 +294,48 @@ L<the C<is rw> trait on classes|/language/typesystem#trait_is_rw> for examples
294294
on how this works on the whole class.
295295
296296
X<|class variables>
297-
A class declaration can also include I<class variables>, which are variables
298-
that are shared by all instances, and can be used for things like counting the
299-
number of instantiations or any other shared state. Class variables use the same
300-
syntax as the rest of the attributes, but are declared as C<my> or C<our>,
301-
depending on the scope; the second type of variables will be used across the
302-
class hierarchy.
297+
A class declaration can also include I<class variables>,
298+
which are variables whose value is shared by all instances, and can be used for
299+
things like counting the number of instantiations or any other shared state.
300+
Class variables use the same syntax as the rest of the attributes, but are
301+
declared as C<my> or C<our>, depending on the scope; C<our> variables will be
302+
shared by subclasses, since they have package scope.
303303
304304
=begin code
305305
class Str-with-ID is Str {
306-
my $.counter = 0;
306+
my $counter = 0;
307+
our $hierarchy-counter = 0;
307308
has Str $.string;
308309
has Int $.ID;
309310
310-
method new( Str $string ) {
311-
self.bless( :$string, ID => $.counter++ )
311+
method TWEAK() {
312+
$!ID = $counter++;
313+
$hierarchy-counter++;
312314
}
315+
316+
}
317+
318+
class Str-with-ID-and-tag is Str-with-ID {
319+
has Str $.tag;
313320
}
314321
315-
say Str-with-ID.new('First').ID; # OUTPUT: «0»
316-
say Str-with-ID.new('Second').ID; # OUTPUT: «1»
322+
say Str-with-ID.new(string => 'First').ID; # OUTPUT: «0␤»
323+
say Str-with-ID.new(string => 'Second').ID; # OUTPUT: «1␤»
324+
say Str-with-ID-and-tag.new( string => 'Third', tag => 'Ordinal' ).ID;
325+
# OUTPUT: «2␤»
326+
say $Str-with-ID::hierarchy-counter; # OUTPUT: «4␤»
317327
=end code
318328
319329
In this case, using C<new> might be the easiest way to initialize the C<$.ID>
320330
field and increment the value of the counter at the same time. C<new>, through
321331
C<bless>, will invoke the default C<BUILD>, assigning the values to their
322332
properties correctly. You can obtain the same effect using C<TWEAK>, which is
323-
considered a more I<p6y> way. Please check L<the section on submethods|/language/classtut#index-entry-OOP>
324-
for an alternative example on how
325-
to do this.
333+
considered a more I<p6y> way. Please check L<the section on
334+
submethods|/language/classtut#index-entry-OOP> for an alternative example on how
335+
to do this. Since C<TWEAK> is called in every object instantiation, it's
336+
incremented twice when creating objects of class C<Str-with-ID-and-tag>; this is
337+
a I<class hierarchy> variable that is shared by all subclasses of
338+
C<Str-with-ID>.
326339
327340
=head1 Static fields?
328341

0 commit comments

Comments
 (0)