www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Create const regex?

reply "AntonSotov" <nepuvive rainmail.biz> writes:
const r1 = regex("bla");
matchFirst( "big string", r1 );  //  ERROR!

immutable r2 = regex("bla"); //  ERROR!

Why can I not use const/immutable regex?
Jun 06 2014
next sibling parent reply Rikki Cattermole <alphaglosined gmail.com> writes:
On 7/06/2014 12:01 a.m., AntonSotov wrote:
 const r1 = regex("bla");
 matchFirst( "big string", r1 );  //  ERROR!

 immutable r2 = regex("bla"); //  ERROR!

 Why can I not use const/immutable regex?
In none of your examples you have not defined the type of the variables. However you are giving it an access modifier. I assume you are wanting auto. import std.regex; void main() { const auto r1 = regex("bla"); pragma(msg, typeof(r1)); } Compilation output: const(Regex!char)
Jun 06 2014
next sibling parent "AntonSotov" <nepuvive rainmail.biz> writes:
On Friday, 6 June 2014 at 12:08:40 UTC, Rikki Cattermole wrote:
 In none of your examples you have not defined the type of the 
 variables. However you are giving it an access modifier.
 I assume you are wanting auto.
no. keyword "const auto" before the variable name - equivalently "const".
Jun 06 2014
prev sibling parent "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
On Friday, 6 June 2014 at 12:08:40 UTC, Rikki Cattermole wrote:
 In none of your examples you have not defined the type of the 
 variables. However you are giving it an access modifier.
 I assume you are wanting auto
D does not require providing a type if const/immutable is use. I think at this point it's changed, but 'auto' comes from C storage classes (which is the default). So by providing a storage class the compiler would infer the type and 'auto' was so descriptive for it.
Jun 07 2014
prev sibling next sibling parent reply "hane" <han.ehit.suzi.0 gmail.com> writes:
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");
Jun 06 2014
parent reply "Meta" <jared771 gmail.com> writes:
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")
Jun 06 2014
parent reply "hane" <han.ehit.suzi.0 gmail.com> writes:
On Friday, 6 June 2014 at 15:42:41 UTC, Meta wrote:
 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")
I see. Thanks.
Jun 06 2014
parent reply "Meta" <jared771 gmail.com> writes:
On Saturday, 7 June 2014 at 00:48:59 UTC, hane wrote:
 On Friday, 6 June 2014 at 15:42:41 UTC, Meta wrote:
 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")
I see. Thanks.
Do you remember where you saw this in Phobos? It was probably unintended, and should be removed.
Jun 07 2014
parent reply "hane" <han.ehit.suzi.0 gmail.com> writes:
On Saturday, 7 June 2014 at 11:33:35 UTC, Meta wrote:
 On Saturday, 7 June 2014 at 00:48:59 UTC, hane wrote:
 On Friday, 6 June 2014 at 15:42:41 UTC, Meta wrote:
 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")
I see. Thanks.
Do you remember where you saw this in Phobos? It was probably unintended, and should be removed.
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?
Jun 07 2014
parent "Meta" <jared771 gmail.com> writes:
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.
Jun 07 2014
prev sibling parent "Rene Zwanenburg" <renezwanenburg gmail.com> writes:
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?
Not sure, but I suspect Regex has some internal state which is mutated during matching.
Jun 06 2014