Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Introduce Attribute will lazy initializer, jnthn++
Inspired by rjbs++, this will actually first wrap the Attribute composer,
and in there, wrap the accessor with code that will call the indicated
block once to set the value, then call the original accessor again.
  • Loading branch information
lizmat committed May 3, 2015
1 parent 065219c commit bdc66e7
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/core/traits.pm
Expand Up @@ -498,7 +498,28 @@ multi sub trait_mod:<handles>(Method:D $m, &thunk) {
}

proto sub trait_mod:<will>(|) { * }
multi sub trait_mod:<will>(Attribute $attr, Block :$build!) {
multi sub trait_mod:<will>(Attribute:D $attr, |c ) {
X::Comp::Trait::Unknown.new(
file => $?FILE,
line => $?LINE,
type => 'will',
subtype => c.hash.keys[0],
declaring => ' attribute',
highexpect => <lazy>,
).throw;
}
multi sub trait_mod:<will>(Attribute:D $attr, $block, :$lazy!) {
$attr does role {
method compose(|) {
callsame();
$attr.package.^method_table{$attr.name.substr(2)}.wrap(-> \self {
once $attr.set_value(self, $block());
callsame();
});
}
}
}
multi sub trait_mod:<will>(Attribute $attr, Block :$build!) { # internal usage
$attr.set_build($build)
}

Expand Down

0 comments on commit bdc66e7

Please sign in to comment.