workaround for closure problem needed
mandel
foo at bar.com
Wed Dec 26 15:17:31 PST 2007
Hi,
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 safe)?
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)
{
Stdout("item").newline;
}
}
More information about the Digitalmars-d-announce
mailing list