@@ -294,35 +294,48 @@ L<the C<is rw> trait on classes|/language/typesystem#trait_is_rw> for examples
294
294
on how this works on the whole class.
295
295
296
296
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 .
303
303
304
304
= begin code
305
305
class Str-with-ID is Str {
306
- my $.counter = 0;
306
+ my $counter = 0;
307
+ our $hierarchy-counter = 0;
307
308
has Str $.string;
308
309
has Int $.ID;
309
310
310
- method new( Str $string ) {
311
- self.bless( :$string, ID => $.counter++ )
311
+ method TWEAK() {
312
+ $!ID = $counter++;
313
+ $hierarchy-counter++;
312
314
}
315
+
316
+ }
317
+
318
+ class Str-with-ID-and-tag is Str-with-ID {
319
+ has Str $.tag;
313
320
}
314
321
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»
317
327
= end code
318
328
319
329
In this case, using C < new > might be the easiest way to initialize the C < $.ID >
320
330
field and increment the value of the counter at the same time. C < new > , through
321
331
C < bless > , will invoke the default C < BUILD > , assigning the values to their
322
332
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 > .
326
339
327
340
= head1 Static fields?
328
341
0 commit comments