www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why can't a Regex object be immutable?

reply Shriramana Sharma <samjnaa_dont_spam_me gmail.com> writes:
Hello. With this code:

import std.stdio, std.regex;
void main()
{
    immutable numbers = regex(r"\d+");
    foreach (match; "a1b2c3d4e5".matchAll(numbers))
	writeln(match[0]);
}

compiling gives the error:

<src>(4): Error: cannot implicitly convert expression (regex("\\d+", "")) of 
type Regex!char to immutable(Regex!char)
<src>(5): Error: template std.regex.matchAll cannot deduce function from 
argument types !()(string, immutable(Regex!char)), candidates are:
/usr/include/dmd/phobos/std/regex/package.d(859):        
std.regex.matchAll(R, RegEx)(R input, RegEx re) if (isSomeString!R && 
is(RegEx == Regex!(BasicElementOf!R)))
/usr/include/dmd/phobos/std/regex/package.d(867):        
std.regex.matchAll(R, String)(R input, String re) if (isSomeString!R && 
isSomeString!String)
/usr/include/dmd/phobos/std/regex/package.d(874):        
std.regex.matchAll(R, RegEx)(R input, RegEx re) if (isSomeString!R && 
is(RegEx == StaticRegex!(BasicElementOf!R)))

If I use `auto` all is fine. Why is it impossible for a Regex object to be 
`immutable`?

-- 

Jan 01 2016
parent reply Shriramana Sharma <samjnaa_dont_spam_me gmail.com> writes:
Shriramana Sharma wrote:

 Why is it impossible for a Regex object to be
 `immutable`?
I find that I can't declare it as `const` either... This is most curious! --
Jan 01 2016
parent reply cym13 <cpicard openmailbox.org> writes:
On Saturday, 2 January 2016 at 02:03:13 UTC, Shriramana Sharma 
wrote:
 Shriramana Sharma wrote:

 Why is it impossible for a Regex object to be
 `immutable`?
I find that I can't declare it as `const` either... This is most curious!
I think it's because regex() only compiles the regex at runtime so it needs to be modified later ; 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.
Jan 01 2016
parent reply Shriramana Sharma <samjnaa_dont_spam_me gmail.com> writes:
cym13 wrote:

 I think it's because regex() only compiles the regex at runtime
 so it needs to be modified later ; 
Aw come on. The immutability of the variable is *after* it has been created at runtime.
 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. --
Jan 01 2016
parent reply cym13 <cpicard openmailbox.org> writes:
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.
Jan 01 2016
parent reply cym13 <cpicard openmailbox.org> writes:
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.
Jan 01 2016
parent Shriramana Sharma <samjnaa_dont_spam_me gmail.com> writes:
cym13 wrote:

 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.
Yes after your comments I realized that it's not so much that I cannot create an immutable or const symbol referring to a Regex object but that I cannot use it with matchAll etc. But of what use is a Regex object if it cannot be used with matchAll etc? --
Jan 01 2016