digitalmars.D.learn - Why can't a Regex object be immutable?
- Shriramana Sharma (26/26) Jan 01 2016 Hello. With this code:
- Shriramana Sharma (4/6) Jan 01 2016 I find that I can't declare it as `const` either... This is most curious...
- cym13 (7/12) Jan 01 2016 I think it's because regex() only compiles the regex at runtime
- Shriramana Sharma (8/14) Jan 01 2016 Aw come on. The immutability of the variable is *after* it has been crea...
- cym13 (17/27) Jan 01 2016 Sure, but still...
- cym13 (19/49) Jan 01 2016 On Saturday, 2 January 2016 at 02:56:35 UTC, cym13 wrote:
- Shriramana Sharma (7/16) Jan 01 2016 Yes after your comments I realized that it's not so much that I cannot
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
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
On Saturday, 2 January 2016 at 02:03:13 UTC, Shriramana Sharma wrote:Shriramana Sharma wrote: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.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
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 mean ctRegex!(), but nope: immutable numbers = ctRegex!r"\d+"; or doing const there gives the same error and using auto doesn't. --you'll find that usingctRegex() 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
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...... 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.You mean ctRegex!(), but nope: immutable numbers = ctRegex!r"\d+"; or doing const there gives the same error and using auto doesn't.you'll find that usingctRegex() 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
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: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.Aw come on. The immutability of the variable is *after* it has been created at runtime.Sure, but still...... 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.You mean ctRegex!(), but nope: immutable numbers = ctRegex!r"\d+"; or doing const there gives the same error and using auto doesn't.you'll find that usingctRegex() 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
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