Non compile time evaluation for initializers

Steven Schveighoffer schveiguy at yahoo.com
Mon Oct 29 07:35:45 PDT 2007


"Bill Baxter" wrote
> I think that's only the case for X x being static in the first place.

Yes, that was my intention.  I was not thinking about instance members, as 
those could be a significant performance hit if you evaluate the rvalue at 
runtime every time an instance is created.

> 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();
>    }
> }

Yeah, it does seem like that would be useful.  I purposely did not include 
instance members because I couldn't think of a good example to explain why 
you would always initialize a variable the same way.  Your example not only 
demonstrates that point, but it is probably a very common one.

I think as long as the user is made aware through documentation that the 
rvalue could be evaluated every time an object is constructed, it would be a 
good thing to have.

-Steve 





More information about the Digitalmars-d mailing list