DMD 0.170 release

Tom S h3r3tic at remove.mat.uni.torun.pl
Tue Oct 17 18:58:25 PDT 2006


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);
	}
}
----



--
Tomasz Stachowiak



More information about the Digitalmars-d-announce mailing list