Skip to content

Commit b371543

Browse files
committed
document Proxy
1 parent 11edcb0 commit b371543

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

WANTED

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ Synatax features:
1919

2020
API docs:
2121
* KeyReducer
22-
* Proxy
2322

2423
Builtins:
2524
* Str.substr-eq

lib/Type/Proxy.pod

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
=begin pod
2+
3+
=TITLE class Proxy
4+
5+
=SUBTITLE Container with custom storage and retrieval
6+
7+
class Proxy { ... }
8+
9+
A Proxy is an object that allows you to execute whenever a value is retrieved
10+
from a contaner (C<FETCH>) or when it is set (C<STORE>).
11+
12+
To create a container that returns twice of what was stored in it, you do
13+
something like this:
14+
15+
sub double() is rw {
16+
my $storage = 0;
17+
Proxy.new(
18+
FETCH => method () { $storage },
19+
STORE => method ($new) { $storage = 2 * $new }
20+
)
21+
}
22+
my $doubled := double();
23+
$doubled = 4;
24+
say $doubled; # 8
25+
26+
=head1 Methods
27+
28+
=head2 method new
29+
30+
method new(:&FETCH!, :&STORE!) returns Proxy:D
31+
32+
Creates a new C<Proxy> object. C<&FETCH> is called with one argument (the
33+
proxy object) when the value is accessed, and must return the value that the
34+
fetch produces. C<&STORE> is called with two arguments (the proxy object, and
35+
the new value) when a new value is stored in the container.
36+
37+
=end pod

0 commit comments

Comments
 (0)