D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7981 - immutable structs by default have mutable constructor
Summary: immutable structs by default have mutable constructor
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-04-24 13:34 UTC by Alex
Modified: 2022-04-03 17:53 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Alex 2012-04-24 13:34:32 UTC
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 tyckesak 2022-04-03 17:53:29 UTC
Old bug, probably fixed as a byproduct of some other contribution.
Works for me at least.