Safer casts

Dee Girl deegirl at noreply.com
Mon May 12 18:31:17 PDT 2008


Sean Kelly Wrote:

> == Quote from Dee Girl (deegirl at noreply.com)'s article
> > Yigal Chripun Wrote:
> > > Dee Girl wrote:
> > > <snip>
> > > maybe I still didn't explain myself properly. I've used the term
> > > "sort-with-delegates" meaning a specific instance of sort when given a
> > > delegate as input. I thought this was clear enough, otherwise I would
> > > have simply said "sort".
> > > before you start sending me assembly code, let's go over it again and
> > > make sure we both understand each other:
> > > you take the sort template, stick in a delegate, mix for five minutes
> > > let is simmer for one more and viola: you got the exact same thing if
> > > you would have written sort as a regular function which receives a
> > > delegate parameter.
> > > the template takes the symbol it receives (the delegate name) and
> > > "embeds" it in the sort instance.
> > > both ways you get:
> > > 1) a call to sort
> > > 2) a call to the delegate
> > > the only difference is the number of parameters the sort function
> > > receives when called.
> > > the regular sort function can run different delegates at runtime (this
> > > is what you call "dynamic". the template has the call to the specific
> > > delegate embedded so for different delegates you need different sort
> > > instances.
> > Good. I think I understand. I want to be very sure, so I have a question from this code.
> > 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?
> 
> It depends what you mean by "powerful."  Passing a comparator as a template parameter,
> as with sort!(), means that the choice of comparator must be made at compile-time rather
> than run-time.  This may be problematic in some instances.

Maybe you did not read previous discussion. Choice of comparator can be made at runtime. sort!(dg) works for a dynamic delegate dg. Just try it! It was hard to believe for me. The way D compiler insantiates sort is ingenious. This makes sort very powerful because it works with both static and dynamic code at the same time! I never found that in a language.

> > 2. Is the call inside sort direct or indirect? Thank you, Dee Girl
> 
> Do you mean inline or not inline?  It's hard to say.  DMD's inlining mechanism is a bit
> quirky, so it could go either way.  For example, I've had struct member functions which
> would be inlined if static and not inlined if not static, and other member functions which
> were the reverse.  I haven't found any way to be sure other than trying it out and examining
> the ASM from the object file.

Question was not about inlining. It was about indirect call with a pointer to function or a regular call. The regular call could be inlined but the pointer call is much harder. The answer is deterministic and very interesting. I think I undestand now how the D compiler generates functions from templates. Thank you, Dee Girl



More information about the Digitalmars-d mailing list