Thread overview | |||||
---|---|---|---|---|---|
|
January 28, 2021 Assigning to class struct member leading to unexpected behavior. | ||||
---|---|---|---|---|
| ||||
I'm experiencing a compile error, but for the life of me, I cannot figure out what is wrong. I'll try to keep it short but the code is roughly as follows: > class Window{ > Screen screen; > alias screen this; > > this() { > Screen s = {bottom_f: {[0, 1]}}; > this.screen = s; // Works > this.screen = {bottom_f: {[0, 1]}}; // Leads to the list of errors below > } > ... > } > struct Screen{ > Vec!(2, int) bottom_f; > ... > } > alias Vec(uint size, Type) = Mat!(size, 1, Type); > > struct Mat(uint rows, uint columns = rows, Type = float) { > private enum uint size = rows * columns; > union{ > Type[columns][rows] mat; > Type[size] vec; > } > static if (columns == 1) alias vec this; > else alias mat this; > . . . > } List of errors: 1. "Label "bottom_f" is not used." 2. "expected ; instead of }" 3. "found `}` when expecting `;` following statement" 4. "use `{ }` for an empty statement, not `;`" 5. "found `void` when expecting `;` following statement" (later at function below constructor) Why would assigning to a member variable lead to errors when using an intermediate variable of the same type does not? I tried tweaking a lot but I just can't figure it out. Am I doing something wrong? |
January 28, 2021 Re: Assigning to class struct member leading to unexpected behavior. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul | On Thursday, 28 January 2021 at 01:43:13 UTC, Paul wrote:
> I'm experiencing a compile error, but for the life of me, I cannot figure out what is wrong.
>
> I'll try to keep it short but the code is roughly as follows:
>> class Window{
>> Screen screen;
>> alias screen this;
>>
>> this() {
>> Screen s = {bottom_f: {[0, 1]}};
>> this.screen = s; // Works
>> this.screen = {bottom_f: {[0, 1]}}; // Leads to the list of errors below
>> }
The braced-initializer syntax only works in declarations, not assignments.
Screen s = {bottom_f: {[0, 1]}}; // Ok - this is a declaration
this.screen = {bottom_f: {[0, 1]}}; // No good - this is an assignment
|
January 28, 2021 Re: Assigning to class struct member leading to unexpected behavior. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul Backus | On Thursday, 28 January 2021 at 02:03:40 UTC, Paul Backus wrote: > The braced-initializer syntax only works in declarations, not assignments. Oh, I see, I'm guessing that explains the (sadly unfinished) In-place struct initialization DIP of wilzbach. (https://github.com/dlang/DIPs/pull/71) Kind of sad so many DIP's seem to end up stranded :/ |
Copyright © 1999-2021 by the D Language Foundation