digitalmars.D.learn - Create const regex?
- AntonSotov (4/4) Jun 06 2014 const r1 = regex("bla");
- Rikki Cattermole (11/15) Jun 06 2014 In none of your examples you have not defined the type of the variables....
- AntonSotov (4/7) Jun 06 2014 no.
- Jesse Phillips (6/9) Jun 07 2014 D does not require providing a type if const/immutable is use. I
- hane (8/12) Jun 06 2014 I think it's a Phobos bug that can't use regex as immutable.
- Meta (5/19) Jun 06 2014 You should not do this, as it will create a new regex everywhere
- Rene Zwanenburg (3/7) Jun 06 2014 Not sure, but I suspect Regex has some internal state which is
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
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
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
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 autoD 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
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
On Friday, 6 June 2014 at 14:25:26 UTC, hane wrote:On Friday, 6 June 2014 at 12:01:55 UTC, AntonSotov 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")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
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
On Saturday, 7 June 2014 at 00:48:59 UTC, hane wrote:On Friday, 6 June 2014 at 15:42:41 UTC, Meta wrote:Do you remember where you saw this in Phobos? It was probably unintended, and should be removed.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 07 2014
On Saturday, 7 June 2014 at 11:33:35 UTC, Meta wrote:On Saturday, 7 June 2014 at 00:48:59 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?On Friday, 6 June 2014 at 15:42:41 UTC, Meta wrote:Do you remember where you saw this in Phobos? It was probably unintended, and should be removed.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 07 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.
Jun 07 2014
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