Skip to content

Instantly share code, notes, and snippets.

@lizmat
Last active September 25, 2019 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lizmat/8c0476706a473405f5b48793eff69f72 to your computer and use it in GitHub Desktop.
Save lizmat/8c0476706a473405f5b48793eff69f72 to your computer and use it in GitHub Desktop.
proof of concept for "is tracked" variable trait
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