View mode: basic / threaded / horizontal-split · Log in · Help
May 13, 2008
Re: Safer casts
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
May 13, 2008
Re: Safer casts
Dee Girl wrote:
> Yigal Chripun Wrote:
> 
>> Dee Girl wrote:
>>
>>>> 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.
>>>
>> OK. so what?
>> either way, It's the compiler/linker job to make optimizations. it sure
>> is more capable and has more info than I am.
>> I'm not implementing the D compiler, I'm using it. my POV is that of the
>> user. I believe Walter is a genius compiler writer and that if it's
>> possible he'll make it happen.
> 
> But this is the interesting thing. Walter is a genius compiler writer and he made it happen already. Now. Your compiler has it already. And it has it in very elegant form. The point I tried many times to clarify is that Walter has done the great work already. But it is not in the documents. Maybe only Walter knows it now. And Andrei because he wrote std.algorithm exactly to use the feature.

In fact, I think this feature was probably added specifically because 
Andrei needed it for std.algorithm.  It has never been discussed or 
documented anywhere, to my knowledge.


Sean
May 13, 2008
Re: Safer casts
Janice Caron wrote:
> 2008/5/13 Sean Kelly <sean@invisibleduck.org>:
>>  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.
> 
> I think you meant to say, the choice of comparator /may/ be made at
> compile-time. Not must.
> 
>     sort!(compare)(array); // compile time
>     sort(compare, array); // run time

I meant the way sort works in std.algorithm.  But it appears that my 
answer was D 1.0-specific.  I had no idea D 2.0 added the option to 
instantiate a template on an alias to a local variable.


Sean
May 13, 2008
Re: Safer casts
Sean Kelly wrote:
> Janice Caron wrote:
>> 2008/5/13 Sean Kelly <sean@invisibleduck.org>:
>>>  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.
>>
>> I think you meant to say, the choice of comparator /may/ be made at
>> compile-time. Not must.
>>
>>     sort!(compare)(array); // compile time
>>     sort(compare, array); // run time
> 
> I meant the way sort works in std.algorithm.  But it appears that my 
> answer was D 1.0-specific.  I had no idea D 2.0 added the option to 
> instantiate a template on an alias to a local variable.

It should work in D1.0, as well. It was added not long before D1.00 came 
out.
May 13, 2008
Re: Safer casts
== Quote from Don (nospam@nospam.com.au)'s article
> Sean Kelly wrote:
> > Janice Caron wrote:
> >> 2008/5/13 Sean Kelly <sean@invisibleduck.org>:
> >>>  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.
> >>
> >> I think you meant to say, the choice of comparator /may/ be made at
> >> compile-time. Not must.
> >>
> >>     sort!(compare)(array); // compile time
> >>     sort(compare, array); // run time
> >
> > I meant the way sort works in std.algorithm.  But it appears that my
> > answer was D 1.0-specific.  I had no idea D 2.0 added the option to
> > instantiate a template on an alias to a local variable.
> It should work in D1.0, as well. It was added not long before D1.00 came
> out.

It doesn't.  Or more specifically, this doesn't work in D 1.0:

   void sort(alias cmp, T)( T buf )
   {
       // pretend we're sorting
       cmp( buf[0], buf[1] );
   }

   void doSort( int[] buf, bool delegate(int,int) cmp )
   {
       sort!(cmp)( buf );
   }


Sean
May 13, 2008
Re: Safer casts
Sean Kelly wrote:
> == Quote from Don (nospam@nospam.com.au)'s article
>> Sean Kelly wrote:
>>> Janice Caron wrote:
>>>> 2008/5/13 Sean Kelly <sean@invisibleduck.org>:
>>>>>  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.
>>>> I think you meant to say, the choice of comparator /may/ be made at
>>>> compile-time. Not must.
>>>>
>>>>     sort!(compare)(array); // compile time
>>>>     sort(compare, array); // run time
>>> I meant the way sort works in std.algorithm.  But it appears that my
>>> answer was D 1.0-specific.  I had no idea D 2.0 added the option to
>>> instantiate a template on an alias to a local variable.
>> It should work in D1.0, as well. It was added not long before D1.00 came
>> out.
> 
> It doesn't.  Or more specifically, this doesn't work in D 1.0:
> 
>     void sort(alias cmp, T)( T buf )
>     {
>         // pretend we're sorting
>         cmp( buf[0], buf[1] );
>     }
> 
>     void doSort( int[] buf, bool delegate(int,int) cmp )
>     {
>         sort!(cmp)( buf );
>     }
> 
> 
> Sean

Oh, *that's* what you guys are talking about?  Here's the bugzilla bug 
for it:

http://d.puremagic.com/issues/show_bug.cgi?id=493

--bb
May 14, 2008
Re: Safer casts
Bill Baxter wrote:
> Sean Kelly wrote:
>> == Quote from Don (nospam@nospam.com.au)'s article
>>> Sean Kelly wrote:
>>>> Janice Caron wrote:
>>>>> 2008/5/13 Sean Kelly <sean@invisibleduck.org>:
>>>>>>  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.
>>>>> I think you meant to say, the choice of comparator /may/ be made at
>>>>> compile-time. Not must.
>>>>>
>>>>>     sort!(compare)(array); // compile time
>>>>>     sort(compare, array); // run time
>>>> I meant the way sort works in std.algorithm.  But it appears that my
>>>> answer was D 1.0-specific.  I had no idea D 2.0 added the option to
>>>> instantiate a template on an alias to a local variable.
>>> It should work in D1.0, as well. It was added not long before D1.00 came
>>> out.
>>
>> It doesn't.  Or more specifically, this doesn't work in D 1.0:
>>
>>     void sort(alias cmp, T)( T buf )
>>     {
>>         // pretend we're sorting
>>         cmp( buf[0], buf[1] );
>>     }
>>
>>     void doSort( int[] buf, bool delegate(int,int) cmp )
>>     {
>>         sort!(cmp)( buf );
>>     }
>>
>>
>> Sean
> 
> Oh, *that's* what you guys are talking about?  Here's the bugzilla bug 
> for it:
> 
> http://d.puremagic.com/issues/show_bug.cgi?id=493

And here I thought D 1.0 just didn't like my aliasing a local variable. 
 Go figure.


Sean
May 18, 2008
Re: Safer casts
Sean Kelly wrote:
> Dee Girl wrote:
>> Yigal Chripun Wrote:
>>
>>> Dee Girl wrote:
>>>
>>>>> 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.
>>>>
>>> OK. so what?
>>> either way, It's the compiler/linker job to make optimizations. it sure
>>> is more capable and has more info than I am.
>>> I'm not implementing the D compiler, I'm using it. my POV is that of the
>>> user. I believe Walter is a genius compiler writer and that if it's
>>> possible he'll make it happen.
>>
>> But this is the interesting thing. Walter is a genius compiler writer 
>> and he made it happen already. Now. Your compiler has it already. And 
>> it has it in very elegant form. The point I tried many times to 
>> clarify is that Walter has done the great work already. But it is not 
>> in the documents. Maybe only Walter knows it now. And Andrei because 
>> he wrote std.algorithm exactly to use the feature.
> 
> In fact, I think this feature was probably added specifically because 
> Andrei needed it for std.algorithm.  It has never been discussed or 
> documented anywhere, to my knowledge.

I take it back.  I created some test code for D 1.0 maybe a year ago 
that uses this feature, so it must have worked in D 1.0 at some point. 
I must be getting old--my memory is going :-p


Sean
June 10, 2008
Re: Safer casts
Sean Kelly wrote:
> 
> So you're saying I could do this:
> 
> void myfunc( bool delegate(int,int) dg )
> {
>     int[] buf = [1,2,3,4,5].dup;
>     sort!(dg)( buf );
> }
> 
> I thought alias parameters had to be resolvable at compile-time.  Interesting.
> 

Hum, I thought the same. And frankly I couldn't see how it could work 
otherwise, so it took me a while, and several experimentations, to 
figure out how it works.
Apparently, if the alias parameter is local, it is as if the template 
body is instantiated in the local scope where the template instance 
occurs, and if the template contains a function, it is as if the 
function used an outer local variable (and thus the function becomes a 
nested function).

After this, didn't took me long to cook up a bug using this feature :P :
http://d.puremagic.com/issues/show_bug.cgi?id=2148

-- 
Bruno Medeiros - Software Developer, MSc. in CS/E graduate
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
Next ›   Last »
13 14 15 16 17
Top | Discussion index | About this forum | D home