Create const regex?

Meta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jun 6 08:42:39 PDT 2014


On Friday, 6 June 2014 at 14:25:26 UTC, hane wrote:
> On Friday, 6 June 2014 at 12:01:55 UTC, AntonSotov wrote:
>> const r1 = regex("bla");
>> matchFirst( "big string", r1 );  //  ERROR!
>>
>> immutable r2 = regex("bla"); //  ERROR!
>>
>> Why can I not use const/immutable regex?
>
> I think it's a Phobos bug that can't use regex as immutable.
>
> You can use const regex with getting rid of "const" attribute 
> with cast().
>     matchFirst( "big string", cast()r1 );
>
> Or using enum seems better way. This style appears in Phobos's 
> code.
>     enum r1 = regex("bla");

You should not do this, as it will create a new regex everywhere 
you use it. Unlike const or immutable, enum in this situation is 
more or less like a C macro.

#define r1 regex("bla")


More information about the Digitalmars-d-learn mailing list