Proxy objects and controlling/monitoring access
Bill Baxter
dnewsgroup at billbaxter.com
Fri Aug 17 14:50:05 PDT 2007
This is something I've been thinking about lately.
Here's something I think is maybe a sort of language challenge akin to
Andrei's "identity" template. It's something that if you can do, opens
up a lot of possibilities, even though it may not be that interesting by
itself.
Ok, so the challenge is, can you make a wrapper around a value that
behaves identically to the value BUT also allows you to monitor each
access? The uses for this are many. In particular observer patterns
and the like often involve getting notifications when things change.
One thing making me thing about this is the enthough.traits framework in
Python that I've been looking at lately. It's a very powerful paradigm
for linking components together. Basically when you use traits, instead
of creating class memebers in the normal way you create them using
something akin to 'my_int = new traits.Int'. From there my_int
basically looks exactly like a regular int to all users, the twist being
that you can easily get a notification every time my_int changes.
So can we do this in D? I guess you might call this "identity with
notifications"
We can certainly do a lot of it using operator overloads. But we can't
do everything. For instance, if you want to wrap a struct there's no
way to emulate getting and setting of the struct's members without
writing a specific wrapper that adds property get/set methods for each
member of the wrapped struct.
--bb
More information about the Digitalmars-d
mailing list