Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Inspired by: https://stackoverflow.com/questions/58055309/a-scalar-with-memory-or-how-to-correctly-tie This allows one to say: class Foo is Proxy { } and implement additional functionality to the Proxy object, like keeping track which values a variable has had (from the SO question). With much help from Jonathan Worthington.
- Loading branch information
Showing
4 changed files
with
40 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
role Perl6::Metamodel::ContainerSpecProtocol { | ||
has $!code_pair; | ||
|
||
method get_container_spec($obj) { | ||
$!code_pair | ||
} | ||
|
||
method set_container_spec($obj, $code_pair) { | ||
$!code_pair := $code_pair; | ||
} | ||
|
||
method publish_container_spec($obj) { | ||
# If we have a container specification here, install that | ||
if $!code_pair { | ||
nqp::setcontspec($obj, 'code_pair', $!code_pair); | ||
$obj.HOW.compose_repr($obj); | ||
} | ||
|
||
# look in the parents for any if none here | ||
else { | ||
for self.mro($obj) -> $class { | ||
if nqp::can($class.HOW, 'get_container_spec') { | ||
my $code_pair := $class.HOW.get_container_spec($class); | ||
if $code_pair { | ||
$!code_pair := $code_pair; | ||
nqp::setcontspec($obj, 'code_pair', $!code_pair); | ||
$obj.HOW.compose_repr($obj); | ||
last; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters