Thread overview
[Issue 6427] New: Templated ctor cannot set immutable member variables
Aug 02, 2011
simendsjo
Aug 02, 2011
Jonathan M Davis
Aug 02, 2011
Jonathan M Davis
Aug 03, 2011
yebblies
August 02, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6427

           Summary: Templated ctor cannot set immutable member variables
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: simendsjo@gmail.com


--- Comment #0 from simendsjo <simendsjo@gmail.com> 2011-08-02 06:39:49 PDT ---
struct S {
    immutable int v;
    this()(int v) {
        this.v = v;
    }
}

void main() {
    S s = S(1);
}

Error: can only initialize const member v inside constructor Error: template instance t.S.__ctor!(int) error instantiating

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 02, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6427


Jonathan M Davis <jmdavisProg@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jmdavisProg@gmx.com


--- Comment #1 from Jonathan M Davis <jmdavisProg@gmx.com> 2011-08-02 11:06:42 PDT ---
1. This compiles with the current compiler on github.

2. I'm not even 100% sure this is supposed to compile, since the constructor isn't immutable. That might only be necessary when the whole struct is immutable though. I don't remember for sure and would have to check in TDPL (which I don't have on me at the moment).

3. While this should certainly compile correctly based on the language spec (or not compile depending on the spec). I would serious advise against having const or immutable variables in a struct, since you can then never assign to them even if they're not const or immutable. If they're on the heap, it's not as big a deal, but a struct like S would be useless in arrays (since all of the values would be stuck as S.init) and every instance of it would be effectively immutable regardless of whether the object itself was typed as immutable or not.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 02, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6427



--- Comment #2 from Jonathan M Davis <jmdavisProg@gmx.com> 2011-08-02 12:45:15 PDT ---
Okay. It looks like immutable constructors only apply to when the struct or class itself that you're constructing is immutable. So, this should definitely compile. And it does on the current version of the compiler. So, if it doesn't compile on 2.054, then this it was fixed since the release. I'd close it, but we might want it end the changelog, so it's probably better that someone who actually works on the compiler closes the bug. Regardless, it works with the latest version of the compiler, so 2.055 shouldn't have this bug.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 03, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6427


yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |yebblies@gmail.com
         Resolution|                            |DUPLICATE


--- Comment #3 from yebblies <yebblies@gmail.com> 2011-08-04 00:37:22 EST ---
*** This issue has been marked as a duplicate of issue 6355 ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------