[Issue 10643] Refused const array struct field initialized with void

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Dec 14 09:05:58 PST 2013


https://d.puremagic.com/issues/show_bug.cgi?id=10643



--- Comment #4 from monarchdodra at gmail.com 2013-12-14 09:05:49 PST ---
(In reply to comment #3)
> Ok, here is what you really mean (i think). Note, that the code fails with any
> initializer (void or some value), so void is irrelevant. Default initialization
> is also irrelevant because you have initializer. So, the issue of interest is: 
> 
> struct S
> {
>     const(int) i = void; // or any other initializer
>     this(int j)
>     {
>         i = j;
>     }
> }
> 
> From changelog: Eventually, they will be deprecated, and then will trigger an
> error. Such fields should now be changed to enum or static. Making a field
> implicitly static based on whether it is const/immutable and has an initializer
> leads to confusion. The static keyword can be used to explicitly make any field
> static.
>
> That is why the code is rejected.

I understand the changelog, and why the code is rejected, but I disagree with
the rationale:
The change is due to the fact that const members were static by default. We are
changing this, however, to make sure people are correctly impacted, we are
making the old semantic explicitly illegal.

*BUT*, if I want to declare an actual struct with a const field (eg: it can't
be changed, but each struct has its own value). I'd want that field's default
value to be "5", but for that value to *possibly* be chosen differently at
declaration. I see no problem with doing this.

However, it seems that even though we have deprecated the "semantic", we still
have the wrong behavior:

> I would add into rationale additional point:
> having initializer and overwriting field is useless because you either know in
> beforehand value of a member (and use initializer), or you do not (and set
> value in RT).

Or... you want to have a default value (5) which is implementation defined,
which the user may or may not decide to not use:

//----
struct S
{
    const(int) i = 5;
}

void main()
{
    S s1; //*Should* be 5
    S s2 = S(6); //*Should* be 6 (doesn't work)
}
//----

If we'd just change right now (still hasn't been done) so that non-statics are
actually... non-static, we wouldn't have this problem.

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


More information about the Digitalmars-d-bugs mailing list