Safer casts

Sean Kelly sean at invisibleduck.org
Tue May 13 08:31:32 PDT 2008


Dee Girl wrote:
> 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.

Regarding the alias stuff I didn't know before.  I don't suppose you 
know why it doesn't work in D 1.0?  It isn't documented in the 2.0 spec 
so either it's a new omission or it's a feature which doesn't require a 
spec change and should therefore be supported in D 1.0 as well.

As for the second question--you're right that I didn't understand what 
you were asking.  Given the classic D 1.0 concept of alias template 
parameters I'd say a direct call occurred, but for some instances (like 
my example), that isn't possible.  So I'd say it depends on what the 
alias refers to, though I haven't looked at the asm to be sure.

>> 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 ^_^

See above.  I believe it's direct when it can be.  Still a bit weird 
that a template can be instantiated with a non-static variable, but the 
approach is certainly sound so who am I to complain :-)


Sean



More information about the Digitalmars-d mailing list