Non compile time evaluation for initializers

Bill Baxter dnewsgroup at billbaxter.com
Fri Oct 26 00:40:57 PDT 2007


Janice Caron wrote:
> On 10/25/07, Steven Schveighoffer <schveiguy at yahoo.com> wrote:
>> Is there a good reason why the compiler does not allow non-CTFE initializers
>> for non-const variables?
> 
> So you're saying that, in general, if
> 
>     X x = y;
> 
> won't compile, then the compiler should rewrite it as
> 
>     X x;
>     static this
>     {
>         x = y;
>     }
> 
> Makes sense to me!

I think that's only the case for X x being static in the first place.
So

   static X x = y;

is rewritten

   static X x;
   static this {
       x = y;
   }

For class members I guess you might have the non-static initializers 
turn into some hidden extra work done just before the class's this() 
constructor.

I don't know if it's a good plan or not, but it would certainly be a lot 
nicer to be able to do:

class x
{
    int[] buffer = new int[128];
    ...
}

instead of:

class x
{
    int[] buffer;

    this() {
       buffer = new int[128];
    }
    this(Arg a) {
       this();
    }
    this(OtherArg a) {
       this();
    }
}

I seem to recall that Java allows that.

--bb



More information about the Digitalmars-d mailing list