Re: Default struct constructors if a struct member is a union

2024-06-29 Thread Basile B. via Digitalmars-d-learn

On Saturday, 29 June 2024 at 23:33:41 UTC, solidstate1991 wrote:

[...]
My question is can I initialize structs like these in one line 
without relying on a second line? My usecase scenario doesn't 
really allow constructors for the struct, since it's a binding 
to an external library via C API.


Apprently that works with an anonymous union:

```d
enum TypeEnum {
Integer32,
Integer64,
Float32,
Float64,
}

struct S {

TypeEnum type;

union {
int i32;
long i64;
float f32;
double f64;
}
}

S foo0 = S(TypeEnum.Integer32, 20);
S foo1 = S(TypeEnum.Integer64, 20L);
```



Default struct constructors if a struct member is a union

2024-06-29 Thread solidstate1991 via Digitalmars-d-learn

```d
union U {
int i32;
long i64;
float f32;
double f64;
}

struct S {
TypeEnum type;
U data;
}

S foo0 = S(TypeEnum.Integer32, S(20));  //Ugly, but works
S foo1 = S(TypeEnum.Integer64, S(20L)); //Error: cannot 
implicitly convert expression

//ditto for the rest of the members
```

My question is can I initialize structs like these in one line 
without relying on a second line? My usecase scenario doesn't 
really allow constructors for the struct, since it's a binding to 
an external library via C API.