Combining Delegate and Functions

Moritz Warning moritzwarning at web.de
Tue Jun 30 16:06:31 PDT 2009


On Tue, 30 Jun 2009 11:38:21 -0400, Jesse Phillips wrote:

> So looking at a post on StackOverflow about D gatchas:
> http://stackoverflow.com/questions/743319/why-isnt-the-d-language-
picking-up/1059780#1059780
> 
> "functions that form closures or are attached to objects (ie. methods)
> are not the same as regular functions, instead they are called
> delegates, and you must be aware of the differences."
> 
> I seem to recall the distinction was going to be going away or
> minimized. Is this already done? Will it be going into D2?

fwiw, that's what I use to convert a function to a delegate (not my 
invention):

R delegate(T) toDg(R, T...)(R function(T) fp)
{
	struct dg
	{
		R opCall(T t)
		{
			return (cast(R function(T)) this) (t);
		}
	}
	
	R delegate(T) t;
	t.ptr = fp;
	t.funcptr = &dg.opCall;
	return t;
}

Note that this won't work for ref arguments.



More information about the Digitalmars-d mailing list