How to use std.meta.Filter?

Uknown sireeshkodali1 at gmail.com
Sun Apr 22 02:13:59 UTC 2018


On Saturday, 21 April 2018 at 17:46:05 UTC, Dr.No wrote:
> On Saturday, 21 April 2018 at 17:15:47 UTC, Jonathan M Davis 
> wrote:
>> On Saturday, April 21, 2018 16:05:22 Dr.No via 
>> Digitalmars-d-learn wrote:
>>> import std.meta : Filter;
>>> enum isNotReservedSymbol(string name) = name != "none" && 
>>> name !=
>>> "lastToken";
>>> enum string[] members = staticMembers!Token;
>>> static foreach(member; Filter!(isNotReservedSymbol, members))
>>> {{
>>>
>>>
>>> This return the error:
>>>
>>>   Error: template instance `pred!(["none", "word", "n", 
>>> "digits",
>>> "name", /* my whole array here */ ])  does not match template
>>> declaration isNotReservedSymbol(string name)
>>>
>>> how should have isNotReservedSymbol be defined?
>>
>> std.meta.Filter operates on an AliasSeq, not a dynamic array. 
>> If you have an array, then you can just use 
>> std.algorithm.iteration.filter with a normal lambda.
>>
>> - Jonathan M Davis
>
> I've tried use normal filter - albeit i'm willing to do all 
> that at runtin, but I got f cannot be read at compile time.
>
> static foreach(member; staticMembers!Token.filter!(f => 
> isNotReservedSymbol!(member))

If you are confused by why some things work during compilation 
and others don't, I would encourage you to read 
https://wiki.dlang.org/User:Quickfur/Compile-time_vs._compile-time
Its a nice article explaining how there are two "compile-time" 
steps where code can be executed.


More information about the Digitalmars-d-learn mailing list