Bug 7981 – immutable structs by default have mutable constructor
Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-04-24T13:34:32Z
Last change time
2022-04-03T17:53:29Z
Assigned to
No Owner
Creator
Alex
Comments
Comment #0 by toriningen.me — 2012-04-24T13:34:32Z
Consider following code:
immutable struct Foo {
this(int) { }
void bar() { }
}
int main(in string[] args) {
auto foo = Foo(1);
foo.bar();
}
This will not work out, because `bar()` is immutable, but `foo` is not. If constructor is declared as `this(int) immutable {}`, it will work again. Default no-arg constructor for immutable structs is already immutable, so shouldn't other constructor also be considered immutable implicitly, much like methods are?
Comment #1 by josipp — 2022-04-03T17:53:29Z
Old bug, probably fixed as a byproduct of some other contribution.
Works for me at least.