Strange compiler error. Whose bug is that?

Oleksii Skidan al.skidan at gmail.com
Sat Jan 27 09:35:05 UTC 2018


On Saturday, 27 January 2018 at 08:18:07 UTC, thedeemon wrote:
> On Friday, 26 January 2018 at 21:17:14 UTC, Oleksii Skidan 
> wrote:
>
>> struct Game {
>>     Triangle player = new Triangle;
>
> When you initialize a struct member like this, compiler tries 
> to calculate the initial value and remember it as data, so each 
> time such struct is constructed the data is just copied. Which 
> means this data must be computable at compile time, however 
> your Triangle constructor is using pointers to some values, 
> these pointers will only be known at run time. This means you 
> need to construct Triangles at run time, in Game constructor, 
> not at compile time in this initialization syntax.

Got it. But are reference-types "computable" at compile time at 
all? Shouldn't they be relying on D runtime?

To my understanding Triangle instantiation happens when Game 
constructor is called. I assume that D runtime has been 
initialized already, and thus there should be a valid GC and it 
should be fine to instantiate a reference-type.

As well, if I'm wrong about Game constructor, then compiler 
generated errors are wrong and misleading. The compiler should be 
swearing at `Triangle player = new Triangle;`, or not?

Thanks,
-- Oleksii



More information about the Digitalmars-d-learn mailing list