[Issue 6174] Initialize const fixed-size array in constructor

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Nov 9 19:29:41 PST 2011


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



--- Comment #3 from bearophile_hugs at eml.cc 2011-11-09 19:29:02 PST ---
See also a comment by Kenji Hara:

http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=148428

> Inside constructor, compiler can detect that this.a[n] is part of the
> fields of 'this' (because static array is value type), then should
> allow to bypass const type checking for assignment.
> But dynamic array and associative array are reference type, then
> compiler cannot detect that the elements of them (this.b[n] and
> this.aa[key]) are part of 'this'. Therefore line 8 and 9 cannot bypass
> const type checking, even if inside constructor, then should be
> rejected.
> 
> I have call this concept "transitively modifiable", and I have
> implemented it with dmd/pull/166.
> "Transitively modifiable" is only checked inside constructor, and
> allow more flexible initializing.
> I think this is reasonable improvement.


So all this is expected to eventually compile and run:


const struct Foo1 {
    const int[1] a;
    this(in int x) pure {
        a[0] = x; // Error
    }
}
const struct Foo2 {
    const int[1] a;
    this(in int x) pure {
        a = [x]; // Error
    }
}
const struct Foo3 {
    const int[1] a;
    this(in int x) pure {
        a[] = x; // Error
    }
}
const struct Foo4 {
    const int a;
    this(in int x) pure {
        a = x; // OK
    }
}
void main() {}

------------------

While this is not yet compilable:


const struct Foo {
    const int[] a;
    const int[int] aa;
    this(in int n) pure {
        this.a = new int[5];
        this.a[0] = n; // line 6
        this.aa[1] = 2; // line 7
    }
}
void main() {}


test.d(6): Error: this.a[0] isn't mutable
test.d(7): Error: this.aa[1] isn't mutable

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