[Request] A way to extract all instance of X from a range

Stefan Koch via Digitalmars-d digitalmars-d at puremagic.com
Tue Mar 15 00:42:58 PDT 2016


On Tuesday, 15 March 2016 at 04:05:11 UTC, Meta wrote:
>
> I believe this should work with the latest DMD:
>
> alias castRange(T) = t => t.map!(x => cast(T) x).filter!(x => x 
> !is null);

No it does not.
However this works :

	auto typeFilter(T, Range)(Range range) {
		import std.algorithm : filter, map;
		import std.traits : ForeachType;
		static assert(is(T == class) && is(T : ForeachType!Range),
			"typeFilter only works with classes that are derived form each 
other");
		return range.map!(x => cast(T) x).filter!(x => x !is null);
	}

You're Welcome.


More information about the Digitalmars-d mailing list