Safer casts

Yigal Chripun yigal100 at gmail.com
Tue May 13 00:59:16 PDT 2008


Dee Girl wrote:
> Janice Caron Wrote:
> 
>> 2008/5/13 Dee Girl <deegirl at noreply.com>:
>>>  int array[] = [1, 2, 3];
>>>  int x = 5;
>>>  sort!((int a, int b) { return a + x < b + x; })(array);
>>>
>>>  Two questions not one ^_^
>>>
>>>  1. Is the code inside sort!() as powerful as delegate?
>> The code inside sort!() /is/ a delegate. It's an anonymous delegate,
>> but its implementation is exactly the same as a delegate with a name.
>> It's equivalent to
>>
>>     int foo (int a, int b) { return a + x < b + x; };
>>     sort!(foo)(array);
> 
> Yes, excellent point. The codes are equivalent. But foo is not a delegate. It is a nested function. Like in Pascal. I am not sure how it can become delegate. Maybe Walter can answer because I see he writes here. Maybe when somebody takes address of foo. Dee Girl

foo _is_ a delegate.
there is no difference: As I explained in a different post, a delegate
is simply two pointers where the context pointer either points to the
surrounding function for nested functions or to the this pointer for
methods.



More information about the Digitalmars-d mailing list