A Proxy is an object that allows you to set a hook that executes whenever a value is retrieved from a container (
FETCH) or when it is set (
STORE). Please note that
Proxy can introduce mutability at places where it would break behavior, e.g. in
To create a container that returns twice what was stored in it, you do something like this:
sub double() is rwmy := double();= 4;say ; # OUTPUT: «8␤»
method new(:!, :! --> Proxy)
Creates a new
&FETCH is called with one argument (the proxy object) when the value is accessed, and must return the value that the fetch produces.
&STORE is called with two arguments (the proxy object, and the new value) when a new value is stored in the container.