Bug 3557 – Struct constructors cannot be declared as pure

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2009-11-29T03:19:00Z
Last change time
2015-06-09T01:27:02Z
Keywords
rejects-valid
Assigned to
nobody
Creator
tomeksowi

Comments

Comment #0 by tomeksowi — 2009-11-29T03:19:44Z
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.
Comment #1 by clugdbug — 2010-01-10T11:54:44Z
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.
Comment #2 by tomeksowi — 2010-01-11T13:33:40Z
(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?
Comment #3 by tomeksowi — 2010-01-11T13:40:33Z
(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); } }
Comment #4 by clugdbug — 2010-01-12T02:20:30Z
(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.
Comment #5 by yebblies — 2011-07-14T21:19:25Z
Marking constructors as pure works in current dmd (2.054). I've put the other case as Issue 6320