Summary: Cannot have pure constructor due to impure invariant
           Product: D
           Version: unspecified
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD

--- Comment #0 from Jonathan M Davis <> 2010-10-02 02:49:43 
PDT ---
This code

struct S
    this(int x) pure
        this.x = x;

        assert(x >= 0);

    int x;

void main()

results in this compilation error

Error: pure function 'this' cannot call impure function '__invariant'
d.d(8): Error: function d.S.__invariant () is not callable using argument types
Error: pure function 'this' cannot call impure function '__invariant'

Maybe this will be fixed by the changes that Don has suggested, I don't know.
But with dmd 2.049, it is, as far as I can tell, impossible to have a pure
constructor and an invariant - and the lack of a pure constructor really limits
how much you can make pure. There doesn't appear to be a way to make invariant
pure either.

So, I see three possible solutions

1. Make it possible to mark invariants as pure.
2. Consider invariant pure as long as it doesn't access mutable globals.
3. Have invariant act like it's pure but don't care whether it really is or
(probably better) output a warning if it isn't.

The one I'd like best would be #3 on the theory that it would allow you to use
writeln() and have a pure invariant. The invariant would be compiled out in
release builds, so it wouldn't affect release code anyway. And if it means that
writeln() doesn't print as often due to a pure function call being optimized
out, then tough luck. It would just be temporary debugging code anyway. And
since there's a halfway decent chance that pure function calls don't get cached
in debug builds anyway, then you really wouldn't lose anything.

Now, assuming that #3 isn't acceptable, #2 would likely be the best, since it
wouldn't require changing the grammar to somehow mark an invariant as pure, but
any of the three would be better than the current situation.

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

Reply via email to