[Issue 11346] [2.064 beta] field initializing not allowed in loops or after labels

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Oct 25 01:29:27 PDT 2013


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



--- Comment #8 from Kenji Hara <k.hara.pg at gmail.com> 2013-10-25 01:29:24 PDT ---
(In reply to comment #6)
> To describe my viewpoint somewhat closer:
[snip]

Unfortunately, AA indexing access and array concatenation also have the
possibility of type system breaking.

struct S {
    immutable int[] arr;
    this(immutable int[] a) {
        arr = a;
        assert(arr.capacity > 0);
        // emulate `arr ~= 10;`
        (* cast(int[]*) &arr) ~= 10;
    }
}
void main() {
    immutable int[] arr = [1,2,3];

    immutable int[] a = arr[0..2];
    assumeSafeAppend(a);
    assert(a.capacity > 0);
    auto s = S(a);

    import std.stdio;
    writeln(arr);   // prints [1,2,10]
}

It's arbitrary example, but under the @system code, concatenation may break
type system without explicit casting.

The AA case is more easy. If you access the AA by the same key twice, you will
mutate once initialized non-mutable data.

struct S {
    immutable int[string] aa;
    this(int n) {
        aa["str"] = n;
        aa["str"] = n;  // multiple initialization
    }
}

Compiler cannot detect above case without extra runtime cost.

Keep the rule for "initialization inside constructor" simple, I think the
current behavior is enough acceptable.

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