www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 18417] New: Make const and immutable postblit constructors


          Issue ID: 18417
           Summary: Make const and immutable postblit constructors illegal
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: issues.dlang jmdavisProg.com

Fundamentally, const and immutable postblit constructors don't work. A postblit
constructor has to mutate the object after it's been copied, which breaks const
and immutable. For const this is a serious problem, because it makes it so that
objects with a postblit constructor don't work with const, but we'd need some
kind of language change to fix that (e.g. introducing some sort of copy
constructor). For immutable, the postblit constructor is pointless, because
there's no reason to do a deep copy of an immutable object. Either way, having
a const or immutable postblit constructor makes no sense, and the errors that
we get when folks try aren't necessarily very clear - e.g. this forum post has
an example of that:

https://forum.dlang.org/post/tzbxajfagwqkebndaqnm forum.dlang.org

struct A {
        this(this) immutable {}

struct B {
        A a;

// Error: immutable method f18.A.__postblit is not callable using a mutable

If I understand correctly, we get the error that we get here becaus __xpostblit
tries to call __postblit with a mutable object, but regardless, the resulting
error message really doesn't tell the programmer what the real problem is. The
real problem is that it doesn't work to declare the postblit constructor as

As such, I propose that we simply make it illegal to mark postblit constructors
with const or immutable. It fundamentally doesn't make sense to mark a postblit
constructor with const or immutable, and if we simply make it illegal, then
folks who try will get an error message about why it can't be done rather than
getting unclear error messages related to the postblit constructor not working.
The real error is that the postblit constructor has been marked improperly, not
that the postblit constructor doesn't work once it's been marked improperly.

Feb 11 2018