[Issue 9665] Structure constant members can not be initialized if have opAssign

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Mar 10 20:40:36 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=9665



--- Comment #16 from Kenji Hara <k.hara.pg at gmail.com> 2013-03-10 20:40:34 PDT ---
(In reply to comment #15)
> From what I understood, the proposal is to relax(remove temporarily) constness
> of members during ctor invocation which allows code like below:
> 
> T* p;
> struct T {
>    void opAssign(int n) { ...; p = &this; }
> }
> struct S {
>    immutable T field;
>    this(...) { field = 1;  // invoke T.opAssign (currently not allowed)
>       /* now global p holds mutable pointer to immutable T object! */
>    }
> }
> 
> Or I misunderstood completely and you really proposing to call something like
> super().

In the raw/cooked design, you cannot call mutable T.opAssign from immutable
field, even inside constructor.

Instead, you should use whole object "assignment" for the field.

  struct S {
     immutable T field;
     this(...) {
        field = immutable(T)(...);  // T's literal or constructor call
        // This _looks like_ "assignment" but in practice
        // it would be treated as initializing. So opAssign is not invoked.
     }
  }

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


More information about the Digitalmars-d-bugs mailing list