CT regex in AA at compile time

MoonlightSentinel moonlightsentinel at disroot.org
Tue Jan 7 15:51:21 UTC 2020


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]+)?`)
     ];
}


More information about the Digitalmars-d-learn mailing list