@@ -13,22 +13,24 @@ result is interesting and, at times, useful.
13
13
14
14
= begin code
15
15
class Task {
16
- has &. callback;
17
- has Task @. dependencies;
16
+ has &! callback;
17
+ has Task @! dependencies;
18
18
has Bool $.done;
19
19
20
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
}
27
29
28
30
method perform() {
29
31
unless $!done {
30
- .perform() for @. dependencies;
31
- &. callback. ();
32
+ .perform() for @! dependencies;
33
+ &! callback();
32
34
$!done = True;
33
35
}
34
36
}
@@ -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