easily convert any method/function to a delegate
Robert Fraser
fraserofthenight at gmail.com
Wed Jul 18 13:57:41 PDT 2007
Thanks! I'm already using something similar, but this should really become a language extension (FPs implicitly converted to delegates).
Daniel Korsgaard Wrote:
> two simple function templates that together will enable you to
> mindlessly use delegates everywhere.
>
> Whee first post on this NG :D
> .. hope you like it..
>
> -------------------
>
> import std.stdio;
>
> void main()
> {
> void delegate() ptr;
>
> void dg()
> {
> writefln("DELEGATE");
> }
>
> ptr = to_delegate(&dg);
> ptr();
>
> ptr = to_delegate(&fp);
> ptr();
> }
>
> void fp()
> {
> writefln("FUNCTION");
> }
>
> /// two functions that will convert any
> R delegate(P) to_delegate(R, P...)(R delegate(P) dg)
> {
> // insanely advanced operation!
> return dg;
> }
>
> /// ditto
> R delegate(P) to_delegate(R, P...)(R function(P) fp)
> {
> R delegate(P) dg;
> dg.funcptr = fp;
> dg.ptr = null;
> return dg;
> }
>
> /+
>
> Output:
> DELEGATE
> FUNCTION
>
> +/
More information about the Digitalmars-d
mailing list