Last active
September 25, 2019 16:21
-
-
Save lizmat/8c0476706a473405f5b48793eff69f72 to your computer and use it in GitHub Desktop.
proof of concept for "is tracked" variable trait
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
class History is Proxy { | |
has @.history; | |
method history() { @.history.skip.map: *<> } | |
method push(\value --> Nil) { @!history.push(value) } | |
method reset(--> Nil) { @!history = () } | |
} | |
sub historized-container() is raw { | |
my $value; | |
my $historized-container := History.new( | |
FETCH => -> $ { $value }, | |
STORE => -> $, \new-value { | |
$historized-container.VAR.push($value); | |
$value = new-value | |
} | |
) | |
} | |
multi sub trait_mod:<is>(Variable:D $variable, Mu :$tracked! --> Nil) is export { | |
(my $name := $variable.var.VAR.name) | |
?? (OUTER::{$name}:exists) | |
?? (OUTER::{$name} := historized-container) | |
!! die $name.substr(1) | |
?? "Could not find container by name '$name'" | |
!! "Can not track containers without name" | |
!! die "Can not track unnamed containers"; | |
} | |
my $alphabet is tracked = 42; | |
$alphabet = 666; | |
dd $alphabet.VAR.history; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment