static class instances not allowed?

Eric eric at makechip.com
Tue Jun 11 09:26:39 PDT 2013


On Tuesday, 11 June 2013 at 16:09:39 UTC, Steven Schveighoffer 
wrote:
> On Tue, 11 Jun 2013 10:04:21 -0400, Eric <eric at makechip.com> 
> wrote:
>
>>
>> The following code does not compile:
>>
>> class Foo { int x; }
>> class Bar { static Foo f = new Foo(); }  // compiler error
>> static Foo g = new Foo(); // compiler error
>
> These can be solved with a static ctor.  Essentially, any 
> static initializers must be evaluatable at compile-time.  I 
> know that in the most recent compiler classes have entered this 
> realm, but I don't know the conditions on when they can be used.
>
> The accepted way:
>
> class Bar {
>   static Foo f;
>   static this() { f = new Foo(); }
> }
>
> static Foo g;
>
> static this()
> {
>    g = new Foo();
> }
>

Great.  This solved my problem.
Many thanks.

-Eric


> -Steve



More information about the Digitalmars-d-learn mailing list