Simplifying a string mixin

Simen Kjærås simen.kjaras at gmail.com
Tue Feb 26 10:09:58 UTC 2019


On Monday, 25 February 2019 at 21:04:48 UTC, Adam D. Ruppe wrote:
> On Monday, 25 February 2019 at 20:57:37 UTC, Victor Porton 
> wrote:
>> Also, what is the most proper thing to check that `name` is a 
>> proper identified (not say !@#)?
>
> just let the compiler do it imo.

Agreed. There are times when error message can be significantly 
more readable though, if testing for valid names is done in 
intelligent places. In this case, something like this can be of 
use:

enum isValidName(string s) = __traits(compiles, { mixin("int 
"~s~";"); });

unittest {
     static assert(isValidName!"a");
     static assert(isValidName!"a_B");
     static assert(!isValidName!"6a");
     static assert(!isValidName!"");
     static assert(!isValidName!"!");
}

--
   Simen


More information about the Digitalmars-d-learn mailing list