diff --git a/lib/Language/classtut.pod b/lib/Language/classtut.pod index 6f4ffd927..d2a8bb7d7 100644 --- a/lib/Language/classtut.pod +++ b/lib/Language/classtut.pod @@ -13,22 +13,24 @@ result is interesting and, at times, useful. =begin code class Task { - has &.callback; - has Task @.dependencies; + has &!callback; + has Task @!dependencies; has Bool $.done; method new(&callback, *@dependencies) { return self.bless(:&callback, :@dependencies); } + submethod BUILD(:&!callback, :@!dependencies) { } + method add-dependency(Task $dependency) { push @!dependencies, $dependency; } method perform() { unless $!done { - .perform() for @.dependencies; - &.callback.(); + .perform() for @!dependencies; + &!callback(); $!done = True; } } @@ -253,6 +255,21 @@ into the C<@dependencies> slurpy array and passes them as named parameters to C (note that C<:&callback> uses the name of the variable--minus the sigil--as the name of the parameter). +X<|BUILD> + +Private attributes really are private. This means that C is not allowed +to bind things to C<&!callback> and C<@!dependencies> directly. To do this, we +override the C submethod, which is called on the brand new object by +C: + + submethod BUILD(:&!callback, :@!dependencies) { } + +Since C runs in the context of the newly created C object, it is +allowed to manipulate those private attributes. The trick here is that the +private attributes (C<&!callback> and C<&!dependencies>) are being used as the +bind targets for C's parameters. Zero-boilerplate initialization! See +L for more information. + =head1 Consuming our class After creating a class, you can create instances of the class. Declaring a