www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Dynamically init a struct with values calculated from other values in the struct?

reply =?iso-8859-1?Q?Robert_M._M=FCnch?= <robert.muench saphirion.com> writes:
I need to dynamically initialize a fixed number of entries of the form: 
(start, length) and iterate over them.

Hence, I think a struct makes sense:

struct S {
   s1, l1
 , s2, l2
 , s3, l3
}

Now I need to initialize the values like this:

S myS = {s1:0, l1:10, s2:(2*s1), ...};

So I somehow would like to reference the prior values to initialize the 
other members. I haven't found a way to do it. Is this possible at all? 
Or do I just init the struct empty and then set the entries?

-- 
Robert M. Münch
http://www.saphirion.com
smarter | better | faster
Apr 04 2020
parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 4/4/20 5:13 AM, Robert M. Münch wrote:
 I need to dynamically initialize a fixed number of entries of the form: 
 (start, length) and iterate over them.
 
 Hence, I think a struct makes sense:
 
 struct S {
    s1, l1
 , s2, l2
 , s3, l3
 }
 
 Now I need to initialize the values like this:
 
 S myS = {s1:0, l1:10, s2:(2*s1), ...};
 
 So I somehow would like to reference the prior values to initialize the 
 other members. I haven't found a way to do it. Is this possible at all? 
I don't think so.
 Or do I just init the struct empty and then set the entries?
 
I recommend using a factory function or ctor. -Steve
Apr 04 2020