Library writing - delegates or fp's?

Carlos Santander csantander619 at gmail.com
Fri Jul 21 09:22:11 PDT 2006


Bruno Medeiros escribió:
> Carlos Santander wrote:
>> Derek Parnell escribió:
>>> Sure you can! How about this ...
>>>
>>>
>>>  import std.stdio;
>>>
>>>  int delegate(int) increment;
>>>
>>>  void main()
>>>  {
>>>     writefln( increment(1) );
>>>     writefln( incr(2) );
>>>  }
>>>
>>>  int incr( int a )
>>>  {
>>>      return a + 1;
>>>  }
>>>
>>>  static this()
>>>  {
>>>      increment = delegate int ( int a ) {return incr(a); };
>>>  }
>>>
>>> Would that do?
>>>  
>>
>> I thought the frame pointer would get lost and cause a segfault later 
>> on (see DFL events handling for an example.) Did that change with 0.161?
>>
> 
> The frame pointer becomes invalid, but that is no problem since the 
> delegate is function-like and does not access the frame context (i.e., 
> it's not a closure).
> 

In this case, yes, you're right. I must admit I didn't notice that. However, in 
other circumstances (more complex, I'd say) it's a valid point to take into account.

-- 
Carlos Santander Bernal



More information about the Digitalmars-d-learn mailing list