dmd 2.063 beta 5

Steven Schveighoffer schveiguy at yahoo.com
Thu May 23 17:33:04 PDT 2013


On Thu, 23 May 2013 19:03:25 -0400, Artur Skawina <art.08.09 at gmail.com>  
wrote:

> On 05/23/13 23:06, Steven Schveighoffer wrote:

>> compiles:
>>
>> struct S
>> {
>>    const int x;
>>    this(int n)
>>    {
>>     x = n;
>>    }
>> }
>
> It's the 'const int x = 42;' case we're talking about. *That* one does  
> not
> compile and doesn't need to. It /could/, but I see no reason to allow  
> this;

My example is essentially:

const int x = 0;

It is default initialized to 0, and then the ctor can initialize it to  
something else.

To say that

const int x = 42;

is different is inconsistent.  Both are consts stored inside each  
instance, written by the compiler before the ctor is called.  Then the  
ctor is free to initialize them away from the default.

What you want is a feature that defines a struct field as always a certain  
value and NOT changeable, even in the ctor.  As far as I know, that does  
not exist in D, and seems specialized to protocol layouts (normally you do  
not want to waste memory with a field that never changes and is always  
known at compile-time).  As the initializer syntax already is taken, you  
will need a new syntax, or improve the optimizer to be able to prove this  
is the case.

-Steve


More information about the Digitalmars-d-announce mailing list