Non compile time evaluation for initializers
Jarrett Billingsley
kb3ctd2 at yahoo.com
Thu Oct 25 21:12:12 PDT 2007
"Steven Schveighoffer" <schveiguy at yahoo.com> wrote in message
news:ffquc2$2kjg$1 at digitalmars.com...
> Is there a good reason why the compiler does not allow non-CTFE
> initializers for non-const variables?
>
> For instance, this does not work:
>
> Logger log = Log.getLogger("test");
>
> but this does:
>
> Logger log;
> static this()
> {
> log = Log.getLogger("test");
> }
I agree. Java and C# allow this, and I think it fits in very nicely with
the existing static constructors. Since you can have multiple static
constructors in a module which are just executed in lexical order, a static
variable being initialized with a non-compile-time initializer could just be
rewritten as a declaration + 1-line static constructor. Basically, your
first snippet would be converted into your second.
More information about the Digitalmars-d
mailing list