AdvancedDelegate - "Partial application" for delegates and function pointers
Markus Dangl
danglm at in.tum.de
Tue Jun 13 15:27:52 PDT 2006
I often miss some features of functional programming languages when
coding in imperative / oo languages. But i really love D, so i had to
write something that allows me to use partial application in D.
What i always wanted to do was: Give another object a delegate which has
a part of it's parameters already given, like this:
void Update(Child c, int a, int b, int c) { ... }
child.OnUpdate = &this.Update(child);
Where the Child wouldn't be able to tell the difference between this
(partially applied) delegate and a delegate of type "void
delegate(int,int,int).
The AdvancedDelegate templates solve this in a type-safe manner. The
only problem remaining was: i had to write different templates for a
different number of parameters. Since this would have been very
repetative, i hacked together a PHP script that does this for me.
Now you can do something like:
child.OnUpdate = new
AdvancedDelegateP4G1!(void,Child,int,int,int)(&this.Update, child);
Where child.OnUpdate would be of the type
"AdvancedDelegate3!(void,int,int,int);"
For more examples, see the "demo.d" file.
The attached ZIP file contains a script (written in PHP because i just
hacked it together) that generates the "advanceddelegate" library for
any given number of parameters. Since not everyone uses PHP, i generated
the library for up to 10 parameters.
You can use delegates and functions with this library, they are fully
interchangeable.
If this is useful in any way, please provide me some feedback - without
feedback i wont be in the mood to develop it any further than it is now ;)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: advanceddelegate.zip
Type: application/octet-stream
Size: 10353 bytes
Desc: not available
Url : http://lists.puremagic.com/pipermail/digitalmars-d-announce/attachments/20060614/828e2a0c/attachment.obj
More information about the Digitalmars-d-announce
mailing list