foreach filter by cast

karl via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Feb 14 16:26:48 PST 2015


Is there a brief way to iterate an array of objects, and process 
only objects of certain type (in either two ways, with/without 
inheritance)?

	foreach(p; obj.children){ // "KObj[] children"
		DPFile dfile = cast(DPFile)p;
		if(!dfile)continue;
		...
	}

I saw the std.algorithm.filter template helper could be used, but 
it's a bit complex:

foreach(unit; file.children.filter!(a => cast(KUnit)a)){
...
}


Thanks


More information about the Digitalmars-d-learn mailing list