[Issue 11343] [2.064 beta] Error: multiple field initialization

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Oct 28 15:19:52 PDT 2013


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


bearophile_hugs at eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs at eml.cc


--- Comment #7 from bearophile_hugs at eml.cc 2013-10-28 15:19:49 PDT ---
(In reply to comment #5)

> A const reference may refer immutable data which comes from the out of the
> constructor. If so, modifying const data inside constructor is definitely
> illegal operation. By disallowing multiple initialization, compiler can
> validate the field initializing correctness or not, at the first initializing
> place.

To reduce a little the disruption of formerly compilable code is it possible
and a good idea to make the detector smarter, and allow code like this?

struct Foo {
    immutable int[3] arr;
    this(int x) {
        arr[0] = x;
        arr[1] = x + 1;
        arr[2] = x + 2;
    }
}
void main() {
    auto f = Foo(5);
}


Currently it gives:

test.d(5): Error: multiple field arr initialization
test.d(6): Error: multiple field arr initialization

The idea is to see and track every cell of a fixed-size array as an independent
variable, like this, that compiles:


struct Foo {
    immutable int arr0, arr1, arr2;
    this(int x) {
        arr0 = x;
        arr1 = x + 1;
        arr2 = x + 2;
    }
}
void main() {
    auto f = Foo(5);
}


If this is possible and good, then I could open an enhancement request on this.

(Once that's possible, in future we could even allow some simple forms of
initialization in loops, but that's for later).

-- 
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