struct A { float f; this (float f) { this.f = f; } static pure A stworz(float f) { return A(f); } } The above doesn't compile: Error: pure function 'stworz' cannot call impure function 'this' What's interesting, if stworz signature is one of the below: static pure stworz(float f); static pure auto stworz(float f); then it merrily compiles. So it's sth about the explicit return type. If I remove the constructor, it also compiles. If A is a class, it also compiles.
It shouldn't compile, since the constructor isn't marked as pure. But if you mark the constructor as pure: pure { this (float f) { this.f = f; } } you get: bug.d(14): Error: cannot modify const/immutable/inout expression this.f So we definitely have a problem.
(In reply to comment #1) > It shouldn't compile, since the constructor isn't marked as pure. But if you > mark the constructor as pure: > > pure { > this (float f) { > this.f = f; > } > } > > you get: > bug.d(14): Error: cannot modify const/immutable/inout expression this.f > > So we definitely have a problem. Yes, I had a feeling compiler should let me have pure ctors.. What about the problem I mentioned at the bottom (if A is a class): class A { float f; this (float f) { // NOT pure this.f = f; } static pure A stworz(float f) { return new A(f); } } This compiles. Should it?
(In reply to comment #2) > This compiles. Should it? I'm now confident it shouldn't: string global; class A { float f; this (float f) { // NOT pure this.f = f; global = "BUGABUGA!"; } static pure A stworz(float f) { return new A(f); } }
(In reply to comment #3) > (In reply to comment #2) > > This compiles. Should it? > > I'm now confident it shouldn't: > > string global; > class A { > float f; > this (float f) { // NOT pure > this.f = f; > global = "BUGABUGA!"; > } > static pure A stworz(float f) { > return new A(f); > } > } That's bug 3573.
Marking constructors as pure works in current dmd (2.054). I've put the other case as Issue 6320