Safer casts

Dee Girl deegirl at noreply.com
Tue May 13 00:36:16 PDT 2008


Yigal Chripun Wrote:

> Dee Girl wrote:
> <snip>
> > 1. Is the code inside sort!() as powerful as delegate?
> >
> >2. Is the call inside sort direct or indirect? Thank you, Dee Girl
> 
> well, you've been answered by an expert (Thanks Sean!)

Definitely Sean is a excellent expert. But my understanding of his answer is a bit different. I see he did not know the answer to the first question. So I gave the answer to him, and he said it is something he did not know before. Then he missed the second question.

> I didn't know that Walter is working on making inlining delegates
> possible, that this is indeed great news. :)

I think this is great but it is much easy said than done. Inlining an indirect call means the callee must be inlined too. Or at least duplicated. There is complicated analysis that shows which indirect calls can be inlined that way. Not all can.

> to answer your questions:
> as I understand it, a delegate is internally a struct with two pointers:
> a context pointer and a function pointer. the context pointer has the
> address of the outer function for nested functions, or the this pointer
> for methods. so, when passing a delegate you pass this struct.
> based on that, I think that the answer to your second question is that
> the call is indirect.

This answer is incorrect. The call is direct. I clarify: this is not matter of optimization or tricks. It is matter of principle. It is coming from the very clever way Walter instantiates templates with alias arguments. He specializes the template at the call site. I wish I had his idea for my project but not it is too late ^_^

I think this is very important feature of the D programming language.

> this is true for both cases, otherwise that trick
> of using a sort!() instance inside a mySort function wouldn't work. all
> you pass to the template version is just a D symbol, in our case the
> name of the delegate. in both cases you need to call that delegate.

In the example I wrote it is not the name of the delegate. It is the name of nested function. It is anonymous but still has internal name.

> you can pass regular function pointers to the template too (they are
> written either with the function reserved word, or with the C syntax)
> and those are simpler to inline. you can overload a non-templated sort
> function to receive those as well.

If a pointer to function is passed then template function is instantiated with that pointer type. Discussion on when that can be inlined takes to another subject.

> I hope, some day function pointers would be implicitly castable to
> delegates (just make the context pointer contain null) and this will
> remove the need for providing two identical functions (one for delegates
> and one for function pointers)
> btw, another way of changing the delegate for a specific sort!()
> instance is simply to assign it; no need for a wrapper function.
> delegate bool(int a, int b) dg;
> dg = aDelegate;
> sort!(dg)(array);
> dg = otherDelegate;
> sort!(dg)(array); // the same instance should be used.

That is correct. The same instance will be used. Thank you, Dee Girl




More information about the Digitalmars-d mailing list