DMD 0.170 release

Bill Baxter dnewsgroup at billbaxter.com
Tue Oct 17 19:20:32 PDT 2006


Tom S wrote:
> Bill Baxter wrote:
>> Bill Baxter wrote:
>>> Is there any reason for not allowing a function to be used too?
>>> Then you could also use a closure as the thing that does the iteration:
>>
>> Ok, I just realized that "delegate" can also be pointer to a 
>> non-static nested function...  Duh.  So my question should have been 
>> -- why doesn't this work?
>>
>> int delegate(int delegate(inout ElemT))
>> reversed(AggregateT,ElemT)(AggregateT array)
>> {
>>     int _innerFunc(int delegate(inout ElemT) loopBody)
>>     {
>>         int done = 0;
>>         for (int i = array.length-1; i >=0; i--)
>>         {
>>             done = loopBody(array[i]);
>>             if (done)
>>                 break;
>>         }
>>         return done;
>>     }
>>     return &_innerFunc;
>> }
>>   ...
>> foreach(real r; reversed!(real[],real)(areal))
>> {...}
>>
>> Compiles but gives:
>> "Error: Access Violation"
>>
>> I'm not totally clear on how pointers/objects are handled in D.  It 
>> looks the call to reversed() is maybe creating a copy of the data?  If 
>> I put in printfs the pointer value is different from that of the 
>> original int[].
> 
> Basically, when you leave 'reversed', any stack data defined within that 
> function becomes invalid, as D doesn't support real closures. What 
> you're aiming for can be achieved without the new feature anyway, e.g. 
> through:
> 
> ----
> import std.stdio;
> 
> 
> struct reverse__(AggregType) {
>     alias typeof(AggregType[0]) ElemType;
>     AggregType arr;
>     int opApply(int delegate(inout ElemType) dg) {
>         int ret = 0;
>         for (int i = arr.length -1; i >= 0; --i){
>             ret = dg(arr[i]);
>             if (ret) break;
>         }
>         return ret;
>     }
> }
> 
> reverse__!(T) reverse(T)(T x) {
>     reverse__!(T) res;
>     res.arr = x;
>     return res;
> }
> 
> 
> void main() {
>     char[] foo = "foo bar";
>     foreach (c; foo.reverse) {
>         writefln(c);
>     }
> }

Did you mean
    foreach (c; reverse(foo)) ??
I guess so.  it does seem to compile that way.

I think this falls into Walter's "dummy classes and hackish stuff" 
category though.

Is there no way to make something similar work with a nested function? 
If my reversed function above could just get the pointer to the actual 
array then it seems like it should work.  Is there some way to declare 
the AggregateT array parameter so that that happens?

--bb



More information about the Digitalmars-d-announce mailing list