Question

In D std.regex.regex() is not pure:

import std.regex;

pure void test() // test.d(5): Error: pure function 'test' cannot call impure function 'regex'
{
    auto r = regex(r"patern123", "g");
}

Why?

Is it

A. Not enough pure keywords were thrown in.

or

B. There exists some deeper reason

Was it helpful?

Solution

std.regex is a module so it can't be marked as pure.

Answer to A: Marking function as pure does not make it pure. It just tells compiler that you would like it to be pure, and it will do the job if the function indeed can be made pure. If it can't be pure, compiler will raise an error. That is the case here - test() can't be pure because std.regex.regex() is not pure. Rule of thumb - a pure function can't call impure function.

Answer to B: Yes, there exist deeper reasons, and I am sure you are probably already aware of them. As a reminder read http://en.wikipedia.org/wiki/Pure_function and ask yourself does your function test() satisfy those two main rules? For this, naturally, you need to understand the semantics of the std.regex.regex() function...

Perhaps you wanted to ask Why is std.regex.regex() not pure? Mat already answered that question I think. - std.regex module does indeed maintain some cache, and it mutates it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top