www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Assigning to class struct member leading to unexpected behavior.

reply Paul <paultjeadriaanse gmail.com> writes:
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?
Jan 27 2021
parent reply Paul Backus <snarwin gmail.com> writes:
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
Jan 27 2021
parent Paul <paultjeadriaanse gmail.com> writes:
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 :/
Jan 27 2021