Thread overview | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
June 06, 2014 Create const regex? | ||||
---|---|---|---|---|
| ||||
const r1 = regex("bla"); matchFirst( "big string", r1 ); // ERROR! immutable r2 = regex("bla"); // ERROR! Why can I not use const/immutable regex? |
June 06, 2014 Re: Create const regex? | ||||
---|---|---|---|---|
| ||||
Posted in reply to AntonSotov | 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)
|
June 06, 2014 Re: Create const regex? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rikki Cattermole | 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".
|
June 06, 2014 Re: Create const regex? | ||||
---|---|---|---|---|
| ||||
Posted in reply to AntonSotov | 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");
|
June 06, 2014 Re: Create const regex? | ||||
---|---|---|---|---|
| ||||
Posted in reply to AntonSotov | 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.
|
June 06, 2014 Re: Create const regex? | ||||
---|---|---|---|---|
| ||||
Posted in reply to hane | 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") |
June 07, 2014 Re: Create const regex? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Meta | 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.
|
June 07, 2014 Re: Create const regex? | ||||
---|---|---|---|---|
| ||||
Posted in reply to hane | 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.
|
June 07, 2014 Re: Create const regex? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Meta | 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?
|
June 07, 2014 Re: Create const regex? | ||||
---|---|---|---|---|
| ||||
Posted in reply to hane | 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.
|
Copyright © 1999-2021 by the D Language Foundation