Choosing between enum arrays or AliasSeqs

Jacob Carlborg via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Aug 25 01:27:41 PDT 2017


On 2017-08-25 08:12, Nordlöw wrote:

> Thanks!
> 
> Your advice led to the following sample solution
> 
> import std.meta : aliasSeqOf;
> immutable englishIndefiniteArticles = [`a`, `an`];
> bool isEnglishIndefiniteArticle(S)(S s)
> {
>      return cast(bool)s.among!(aliasSeqOf!englishIndefiniteArticles);
> }
> 
> Is this the preferred way?

Since you're converting the returned index to a bool, can't you use 
"canFind" instead?

immutable englishIndefiniteArticles = [`a`, `an`];
bool isEnglishIndefiniteArticle(S)(S s)
{
     return englishIndefiniteArticles.canFind(s);
}

> Could a template-parameter overload to `among` (perhaps calling it 
> something slighty different) be defined that takes an immutable array as 
> argument to prevent the conversion to the `AliasSeq` prior to the call?

I guess. Currently none of the existing overloads take an array, 
immutable or otherwise. They expect the values to be give as separate 
arguments.

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list