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

Meta via Digitalmars-d digitalmars-d at puremagic.com
Tue Mar 15 04:23:22 PDT 2016


On Tuesday, 15 March 2016 at 07:42:58 UTC, Stefan Koch wrote:
> 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.

What version of DMD are you using? It compiles and a small test 
works for me on 2.070.2.


More information about the Digitalmars-d mailing list