Why can't a Regex object be immutable?

cym13 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jan 1 19:13:52 PST 2016


On Saturday, 2 January 2016 at 02:56:35 UTC, cym13 wrote:
> On Saturday, 2 January 2016 at 02:39:36 UTC, Shriramana Sharma 
> wrote:
>> Aw come on. The immutability of the variable is *after* it has 
>> been created at runtime.
>
> Sure, but still...
>
>>> > you'll find that using
>>> ctRegex() instead will allow you to declare it immutable for 
>>> example. I didn't look at the implementation to identify a 
>>> precise cause though.
>>
>> You mean ctRegex!(), but nope:
>>
>>     immutable numbers = ctRegex!r"\d+";
>>
>> or doing const there gives the same error and using auto 
>> doesn't.
>
> ... I definitely get no error with this line (DMD v2.069, GDC 
> 5.3.0, LDC
> 0.16.1). The exact code I used is below.
>
>     void main(string[] args) {
>         import std.regex;
>         immutable numbers = ctRegex!r"\d+";
>     }
>
> So yes immutability occurs after its creation, but it clearly 
> seems linked to
> a runtime-related issue nonetheless. I don't know what you used 
> to get an
> error with ctRegex as I couldn't reproduce one, maybe the 
> solution lies
> there.

On Saturday, 2 January 2016 at 02:56:35 UTC, cym13 wrote:
[...]

While playing with your original code, I realised that maybe what 
you meant by "the same error" is the « Error: template 
std.regex.matchAll cannot deduce function from argument types 
!()(string, const(StaticRegex!char)) » one. But that error has 
nothing to do with the first one (« Error: cannot implicitly 
convert expression (regex("\\d+", "")) of type Regex!char to 
immutable(Regex!char) ») which is far more interesting.

So my question would be, what's your problem? Is it that you 
can't make an immutable regex()? In that case it is a 
runtime-related issue and those variables just have to be 
mutable. Or is it that you want to be able to use an immutable or 
const regex (be it from regex() or ctRegex!()) with matchAll()? 
In the latter case it is matchAll's fault for not garanteeing the 
immutability of the regex (and may even qualify as a bug IMHO) 
but you can « cast(Regex!char)numbers » it if you must so it 
isn't hard to work arround it.


More information about the Digitalmars-d-learn mailing list