Static struct initialization syntax behavior & it being disabled upon adding a constructor
HuskyNator
HuskyNator at protonmail.ch
Sun Apr 17 15:13:29 UTC 2022
This is a twofold question, along the example code below:
- 1: Why does `m` initialization behave as if `m[0][]=1` and
`m[1][]=2` were used? (Shouldn't this code result in an error
instead?)
- 2: Why does adding a constructor to a struct disable the use of
the static initialization syntax? I only see it mentioned in the
documentation indirectly (there are notes in the example code
specifying as such, but the text itself does not seem to define
their removal). I also don't see how this behavior is beneficial,
as it now requires me to write additional constructors, as soon
as I want to add 1.
```d
struct Mat(int n){
int[n][n] mat;
void write(){
writeln(mat);
}
// Will cause the m & n initialisations to yield errors.
// this(int i){
// mat[0][0] = i;
// }
}
void main() {
Mat!2 m = {[1,2]}; // Prints [[1, 1], [2, 2]]
Mat!2 n = {[[1,2],[3,4]]}; // Prints [[1, 2], [3, 4]]
m.write();
n.write();
}
```
PS:
Are there any plans to change the behaviour of empty struct
constructors? (eg: `this(){}`) It surprised me greatly coming
into D, and one of the commonly suggested workarounds (using
`opCall`) seems rather inelegant to me. Why is this possible in
C++ in contrast?
More information about the Digitalmars-d-learn
mailing list