easily convert any method/function to a delegate

Daniel Korsgaard d__s__k at hotmail.com
Wed Jul 18 12:17:50 PDT 2007


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