www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - workaround for closure problem needed

reply mandel <foo bar.com> writes:

I have a problem with closures and D 1.024.
D2 does have full closure support, but I have to stick with D1
for now.

Problem: variable "state" changes silently when the anonymous
function goes out of the scope of function filter.
Therefore the program does print out "item" only once.

My question is if there is a workaround ("static state" wouldn't be thread

import tango.io.Stdout;
import tango.util.collection.iterator.FilteringIterator;
import tango.util.collection.iterator.ArrayIterator;
import tango.util.collection.model.Iterator;

class A
	uint getState() { return 0; }

Iterator!(A) filter(Iterator!(A) iter)
	uint state = 0;
	return new FilteringIterator!(A)( iter, (A item) { 
		return (item.getState == state); //should always return true

void main(char[][] args)
	A[] aa = new A[10];
	foreach(inout a; aa) a = new A;
	auto array_iter = new ArrayIterator!(A)(aa);
	auto iter = filter(array_iter);
	foreach(a; iter)
Dec 26 2007
parent mandel <foo bar.com> writes:
sorry, wrong list.
Dec 26 2007