Create const regex?

Meta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jun 7 12:03:28 PDT 2014


On Saturday, 7 June 2014 at 16:15:47 UTC, hane wrote:
> At std.regex.
>
>
> BTW, I found that immutable regex can be created with enum.
>     enum r1_ = regex("bla");
>     immutable r1 = r1_;
> Regex struct created during compiling can be immutable?

In this case, it must be using enum to force CTFE. As for the 
immutable variable, in D, unique expressions can be implicitly 
casted to immutable. A unique expression is, roughly, a 
newly-created value that is not referenced anywhere else in the 
program. What that means is that it's legal to implicitly convert 
it to immutable, since there is no other references to that value 
which could modify it. The expression `regex("bla")` is a unique 
expression, and can therefore be implicitly casted to immutable. 
Whether the assignment to an immutable variable is happening at 
runtime or compile-time though, I don't know.


More information about the Digitalmars-d-learn mailing list