CT regex in AA at compile time

Taylor Hillegeist taylorh140 at gmail.com
Tue Jan 7 16:00:37 UTC 2020


On Tuesday, 7 January 2020 at 15:51:21 UTC, MoonlightSentinel 
wrote:
> On Tuesday, 7 January 2020 at 15:40:58 UTC, Taylor Hillegeist 
> wrote:
>> but I can't get it to work. it says its an Error: non-constant 
>> expression.
>>
>> I imagine this has to do with the ctRegex template or 
>> something. maybe there is a better way? Does anyone know?
>
> This issue is unrelated to ctRegex, AA literals are 
> non-constant expressions (probably due to their 
> implementation). You can work around this by using module 
> constructors or lazy initialisation inside of a function:
>
> static Regex!char[TokenType] Regexes;
>
> shared static this()
> {
>     Regexes = [
>         TokenType.Plus:   ctRegex!(`^ *\+`),
>         TokenType.Minus:  ctRegex!(`^ *\-`),
>         TokenType.LPer:   ctRegex!(`^ *\(`),
>         TokenType.RPer:   ctRegex!(`^ *\)`),
>         TokenType.Number: ctRegex!(`^ *[0-9]+(.[0-9]+)?`)
>     ];
> }

Thank you for bringing this to my attention.

also, the solution you used is very cool. I had no idea you could 
put shared static this()
just anywhere and have it execute just like it was in main! Does 
this work for dlls as well?


More information about the Digitalmars-d-learn mailing list