Summary: Enforcement and purity
           Product: D
           Version: unspecified
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos

--- Comment #0 from 2012-09-10 02:58:42 PDT ---
The argument taken by enforce must be "castable to bool", so the the
implementation can do the cast. However, enforce is declared pure, so if the
cast operator is not pure, the compilation fails:

import std.regex;
import std.exception;
void main()
    auto m = match("hello world", regex("world"));
    assert(m); //<- Fine
    enforce(m); //Here
Error: pure function 'enforce' cannot call impure function '~this'
Error: pure function 'enforce' cannot call impure function 'opCast'

The messages come from:

T enforce(T)(T value, lazy const(char)[] msg = null, string file = __FILE__,
size_t line = __LINE__) @safe pure
    if (!value) bailOut(file, line, msg);
    return value;
"if(!value)": This makes an impure call to opCast.

"enforce(T)(T value..." This uses pass by value, and makes an impure call to
the destructor

I have no idea what a good fix would be. Regarding pass by value, wouldn't this
be a textbook example of using "auto ref" with "auto return"? I have no idea...

Configure issuemail:
------- You are receiving this mail because: -------

Reply via email to