digitalmars.D.learn - Union Initialization
- Rubn (16/16) Jan 30 2018 Is there any way to initialize an array of unions with more than
 - =?UTF-8?Q?Ali_=c3=87ehreli?= (14/38) Jan 30 2018 You can have explicit constructors:
 
Is there any way to initialize an array of unions with more than 
just the first union type?
struct A { float a; }
struct B { uint b; }
union Test
{
     A a;
     B b;
}
Test[2] test =
[
     Test(A(1.0f)),
     Test(B(10)), // ERROR
];
AFAIK there's no way to specify to use D with an initializer:
Test test = { b: B(10) };
 Jan 30 2018
On 01/30/2018 07:33 PM, Rubn wrote:
 
 Is there any way to initialize an array of unions with more than just 
 the first union type?
 
 
 struct A { float a; }
 struct B { uint b; }
 
 union Test
 {
      A a;
      B b;
 }
 
 Test[2] test =
 [
      Test(A(1.0f)),
      Test(B(10)), // ERROR
 ];
 
 
 AFAIK there's no way to specify to use D with an initializer:
 
 Test test = { b: B(10) };
You can have explicit constructors:
union Test
{
     A a;
     B b;
     this(A a) {
         this.a = a;
     }
     this(B b) {
         this.b = b;
     }
}
Ali
 Jan 30 2018








 
 
 
 =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com>