@@ -17,10 +17,12 @@ result is interesting and, at times, useful.
17
17
has Task @!dependencies;
18
18
has Bool $.done;
19
19
20
- method new(&callback, Task *@dependencies) {
20
+ method new(&callback, *@dependencies) {
21
21
return self.bless(:&callback, :@dependencies);
22
22
}
23
23
24
+ submethod BUILD(:&!callback, :@!dependencies) { }
25
+
24
26
method add-dependency(Task $dependency) {
25
27
push @!dependencies, $dependency;
26
28
}
@@ -253,6 +255,21 @@ into the C<@dependencies> slurpy array and passes them as named parameters to
253
255
C < bless > (note that C < :&callback > uses the name of the variable--minus the
254
256
sigil--as the name of the parameter).
255
257
258
+ X < |BUILD >
259
+
260
+ Private attributes really are private. This means that C < bless > is not allowed
261
+ to bind things to C < &!callback > and C < @!dependencies > directly. To do this, we
262
+ override the C < BUILD > submethod, which is called on the brand new object by
263
+ C < bless > :
264
+
265
+ submethod BUILD(:&!callback, :@!dependencies) { }
266
+
267
+ Since C < BUILD > runs in the context of the newly created C < Task > object, it is
268
+ allowed to manipulate those private attributes. The trick here is that the
269
+ private attributes (C < &!callback > and C < &!dependencies > ) are being used as the
270
+ bind targets for C < BUILD > 's parameters. Zero-boilerplate initialization! See
271
+ L < objects|/language/objects#Object Construction > for more information.
272
+
256
273
= head1 Consuming our class
257
274
258
275
After creating a class, you can create instances of the class. Declaring a
0 commit comments