Safer casts

Dee Girl deegirl at noreply.com
Tue May 13 01:15:37 PDT 2008


Yigal Chripun Wrote:

> 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.

It is clear what is a delegate. It is pointer to a function and pointer to environment. Like in Lisp. 

But foo is not delegate. It is nested function. Like in Pascal. If you call foo from the same scope there is regular function call. Not delegate call! If you take address &foo it becomes delegate. (I just checked). D has very interesting scheme. I hope Walter writes here and confirms. But your explanation is wrong. I hope you are not offended. I am sorry, Dee Girl




More information about the Digitalmars-d mailing list