How to create a template class using foreach delegate to filter objects in a member function call?

Robert M. Münch robert.muench at saphirion.com
Thu May 30 18:34:31 UTC 2019


I have myClass and I want to add a way where I can provide a delegate 
to iterate over myClass.objects when a member function put(...) of 
myClass is called. The idea is that a user of myClass can provide 
something like an "iterator filter" so that the function is only called 
on a subset of myClass.objects.


myClass(E){
	myOtherClass!E objects;

	void put(E obj) {
		.put(objects, obj);
	}

	void put(T)(E obj, T filter) {
		foreach(o ; filter!E(objects)){
			.put(o, obj);
		}
	}
}

struct myFilter {
	myOtherClass!E object;

	opApply(int delegate(E) foreach_body) const {
		if(mytestfunc(object))
			return(foreach_body(object));
	}
}

But I'm struggeling how to write all this down with tempaltes, because 
E is not known in myFilter. But the filter code needs to be aware of 
the template type if myClass. I hope the idea want to do is 
understandable.

-- 
Robert M. Münch
http://www.saphirion.com
smarter | better | faster



More information about the Digitalmars-d-learn mailing list