Thread overview
static const (D 1.x)
Sep 19, 2007
Bill Baxter
Sep 21, 2007
Don Clugston
Sep 21, 2007
Bill Baxter
September 19, 2007
I've got a situation like this:

struct A
{
  opEquals
  int idx = -1;
}


class Foo(Type)
{
   ...
   static const A  StaticA;
}


But it doesn't work when I try to do something like this in a function:


   A myA;

   if (myA == Foo!(Type).StaticA) { // <-- error here
   }

The error I get is:
   Error: can only initialize static const StaticA inside static constructor

Is this a bug?
I notice that it compiles if I make StaticA just "static" instead of "static const".

At the very least the compiler error text seems wrong since I'm not trying to initialize anything.

--bb
September 21, 2007
Bill Baxter wrote:
> I've got a situation like this:
> 
> struct A
> {
>   opEquals
>   int idx = -1;
> }
> 
> 
> class Foo(Type)
> {
>    ...
>    static const A  StaticA;
> }
> 
> 
> But it doesn't work when I try to do something like this in a function:
> 
> 
>    A myA;
> 
>    if (myA == Foo!(Type).StaticA) { // <-- error here
>    }
> 
> The error I get is:
>    Error: can only initialize static const StaticA inside static constructor
> 
> Is this a bug?
> I notice that it compiles if I make StaticA just "static" instead of "static const".
> 
> At the very least the compiler error text seems wrong since I'm not trying to initialize anything.
> 
> --bb

Is StaticA getting initialized somewhere?
If not, that's probably the root cause (and the error message is a bit misleading).
September 21, 2007
Don Clugston wrote:
> Bill Baxter wrote:
>> I've got a situation like this:
>>
>> struct A
>> {
>>   opEquals
>>   int idx = -1;
>> }
>>
>>
>> class Foo(Type)
>> {
>>    ...
>>    static const A  StaticA;
>> }
>>
>>
>> But it doesn't work when I try to do something like this in a function:
>>
>>
>>    A myA;
>>
>>    if (myA == Foo!(Type).StaticA) { // <-- error here
>>    }
>>
>> The error I get is:
>>    Error: can only initialize static const StaticA inside static constructor
>>
>> Is this a bug?
>> I notice that it compiles if I make StaticA just "static" instead of "static const".
>>
>> At the very least the compiler error text seems wrong since I'm not trying to initialize anything.
>>
>> --bb
> 
> Is StaticA getting initialized somewhere?
> If not, that's probably the root cause (and the error message is a bit misleading).

It's not getting initialized anywhere, but it's a struct, so it has a default value with which it should be implicitly initialized.

Are you saying that I should be doing
    static const A StaticA = {-1};
?
I'll give it a try, but it doesn't seem like that should be necessary.
--bb