Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
document Proxy
- Loading branch information
Showing
2 changed files
with
37 additions
and
1 deletion.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,6 @@ Synatax features: | |
|
|
||
| API docs: | ||
| * KeyReducer | ||
| * Proxy | ||
|
|
||
| Builtins: | ||
| * Str.substr-eq | ||
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,37 @@ | ||
| =begin pod | ||
| =TITLE class Proxy | ||
| =SUBTITLE Container with custom storage and retrieval | ||
| class Proxy { ... } | ||
| A Proxy is an object that allows you to execute whenever a value is retrieved | ||
| from a contaner (C<FETCH>) or when it is set (C<STORE>). | ||
| To create a container that returns twice of what was stored in it, you do | ||
| something like this: | ||
| sub double() is rw { | ||
| my $storage = 0; | ||
| Proxy.new( | ||
| FETCH => method () { $storage }, | ||
| STORE => method ($new) { $storage = 2 * $new } | ||
| ) | ||
| } | ||
| my $doubled := double(); | ||
| $doubled = 4; | ||
| say $doubled; # 8 | ||
| =head1 Methods | ||
| =head2 method new | ||
| method new(:&FETCH!, :&STORE!) returns Proxy:D | ||
| Creates a new C<Proxy> object. C<&FETCH> is called with one argument (the | ||
| proxy object) when the value is accessed, and must return the value that the | ||
| fetch produces. C<&STORE> is called with two arguments (the proxy object, and | ||
| the new value) when a new value is stored in the container. | ||
| =end pod |