Combining Delegate and Functions

Moritz Warning moritzwarning at web.de
Fri Jul 3 12:59:18 PDT 2009


On Fri, 03 Jul 2009 15:30:27 -0400, Eric Poggel wrote:

> Moritz Warning wrote:
>> 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.
> 
> I use this too, but my point was that it'd be nice if hacks like this
> weren't required, e.g. by doing away with functions except for C
> compatibility and making everything a delegate internally.

I would like to omit the use of workarounds like this
and be able to implicitly convert functions to delegates.

I don't feel comfortable with dropping functions altogether.



More information about the Digitalmars-d mailing list