Why is it difficult to reference arrays in structs?

Brett Brett at gmail.com
Thu Oct 3 04:32:52 UTC 2019


struct Y { double q; }
struct X { Y[] a; }

X x;

auto r = x.a;

r is not a reference?!?!


When updating x.a, r is not updated ;/ [I'm not sure if it just 
creates a slice or what]

Ok, fine!

auto r = &x.a;

Now r is a reference, great!.

But now the semantics of using the array completely change and 
errors abound!

Normal D pointers tend to act very nice, e.g., we don't have to 
use the C++ -> BS....

But with arrays it seems things are all screwed up and do not 
work correctly.

I can't just use r like I used x.a. I can't even use *r as things 
then get even more screwed up.

I want to have a slice of an array that I can append to as a sort 
of temporary and have it append to the main array(the end of the 
slice always is the end of the main array)..

I've tried assumeSafeAppend but it does nothing to help.

The reason is because i have an algorithm that generates data and 
stores it in an array and it is more efficient and easier to code 
if I can just append data to a "slice" of the array(there are no 
issues with overwriting).

Essentially it just acts as an alias.

By using pointers it would work but it screws up the entire 
syntax of all the code and then still doesn't work.









More information about the Digitalmars-d-learn mailing list