Jump to page: 1 2
Thread overview
[Issue 6174] New: Initialize const fixed-size array in constructor
Aug 25, 2011
Trass3r
Nov 12, 2011
Kenji Hara
Oct 07, 2012
yebblies
Oct 08, 2012
Kenji Hara
June 18, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6174

           Summary: Initialize const fixed-size array in constructor
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2011-06-18 04:31:54 PDT ---
This program shows that you are allowed to initialize a single const char inside static this() and struct constructor, but you are not allowed to initialize the items of a char array:


const char gc;
const char[1] ga;
static this() {
    gc = 'a';    // OK
    ga[0] = 'a'; // line 5, Err
}
struct Foo {
    const char cc;
    const char[1] array;
    this(char c) {
        cc = c;       // OK
        array = [c];  // line 12, Err
        array[0] = c; // line 12, Err
    }
}
void main() {}


DMD 2.053:
temp.d(5): Error: ga[0] isn't mutable
temp.d(12): Error: slice this.array[] is not mutable
temp.d(13): Error: this.array[0] isn't mutable

I'd like to initialize the arrays too.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 25, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6174


Trass3r <mrmocool@gmx.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |rejects-valid
                 CC|                            |mrmocool@gmx.de
           Platform|x86                         |All
         OS/Version|Windows                     |All
           Severity|enhancement                 |normal


--- Comment #1 from Trass3r <mrmocool@gmx.de> 2011-08-24 17:19:30 PDT ---
This also happens without explicit slicing/indexing:

immutable float[3] foo;

static this()
{
    foo = [1,2,3];
}

In my actual code I need a function to initialize the array.
It uses exp and thus isn't CTFEable. Hence static this would be the only to
initialize this immutable.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 10, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6174



--- Comment #3 from bearophile_hugs@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: -------
November 12, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6174



--- Comment #4 from Kenji Hara <k.hara.pg@gmail.com> 2011-11-12 06:56:29 PST ---
*** Issue 6924 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 10, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=6174


hsteoh@quickfur.ath.cx changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |hsteoh@quickfur.ath.cx


--- Comment #5 from hsteoh@quickfur.ath.cx 2012-04-09 19:42:42 PDT ---
*** Issue 7882 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 07, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=6174


yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |yebblies@gmail.com
         Resolution|                            |FIXED


--- Comment #6 from yebblies <yebblies@gmail.com> 2012-10-08 05:48:48 EST ---
https://github.com/D-Programming-Language/dmd/commit/5b42e51481d186ee5e3c2684a237a05cea33a0cf

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 07, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=6174



--- Comment #7 from bearophile_hugs@eml.cc 2012-10-07 13:01:14 PDT ---
This is an improvement of D.


std.algorithm.copy can't be used to initialize const array fields, but this is not important:


import std.algorithm: copy;
class Foo {
    const int[3] bar;
    this() {
        auto data = [10, 20, 30];
        foreach (i, d; data) bar[i] = d; // OK
        copy(data, bar[]); // error
    }
}
void main() {}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 07, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=6174



--- Comment #8 from bearophile_hugs@eml.cc 2012-10-07 13:12:27 PDT ---
Is this expected? Do you want me to put this in a new bug report?


struct Foo {
    int[1] bar;
}
const Foo[1] foos;
static this() {
    foreach (i; 0 .. foos.length)
        foos[i].bar[i] = 1; // OK
    foreach (i, ref f; foos)
        f.bar[i] = 1; // Error
}
void main() {}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 08, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=6174



--- Comment #9 from Kenji Hara <k.hara.pg@gmail.com> 2012-10-08 05:35:43 PDT ---
(In reply to comment #8)
> Is this expected? Do you want me to put this in a new bug report?
> 
> struct Foo {
>     int[1] bar;
> }
> const Foo[1] foos;
> static this() {
>     foreach (i; 0 .. foos.length)
>         foos[i].bar[i] = 1; // OK
>     foreach (i, ref f; foos)
>         f.bar[i] = 1; // Error
> }
> void main() {}

Hmm, it's interesting. The accessed memory through the reference 'f' would specify a part of the constructed fields, and compiler would be able to detect it in statically.

Please put in a new report.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 08, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=6174



--- Comment #10 from bearophile_hugs@eml.cc 2012-10-08 10:22:27 PDT ---
(In reply to comment #9)

> Please put in a new report.

OK, Issue 8783
Thank you.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
« First   ‹ Prev
1 2