digitalmars.D.learn - difficulty with rectangular arrays
hullo all. i've encountered a bizzare inconsistency. the following is the [D spec on rectangular arrays](https://dlang.org/spec/arrays.html#rectangular-arrays): ``` void main() { import std.stdio: write, writeln, writef, writefln; double[6][3] matrix = 0; // Sets all elements to 0. writeln(matrix); // [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]] } ``` however, when i attempt to place the very same code within a class... ``` class ExampleClass { double[6][3] matrix = 0; //fails to compile - "Error: cannot implicitly convert expression `0` of type `int` to `double[6][3]`" } ``` evidently i'm doing something wrong here, but i can't understand what or why. what's going on? have i misread the spec?
Jun 11 2021
On Friday, 11 June 2021 at 08:30:29 UTC, Moth wrote:``` class ExampleClass { double[6][3] matrix = 0; //fails to compile - "Error: cannot implicitly convert expression `0` of type `int` to `double[6][3]`" } ``` evidently i'm doing something wrong here, but i can't understand what or why. what's going on? have i misread the spec?The example in the spec is in a function body and you've copied it to a class body, where the writeln() would also be in error. I find https://dlang.org/spec/grammar.html quite hard to read but I imagine there's a state/declaration distinction there, despite the code looking the same. This works: ```d class Example { double[6][3] matrix; this() { matrix = 0; } } ```
Jun 11 2021
On Friday, 11 June 2021 at 08:40:51 UTC, jfondren wrote:The example in the spec is in a function body and you've copied it to a class body, where the writeln() would also be in error. I find https://dlang.org/spec/grammar.html quite hard to read but I imagine there's a state/declaration distinction there, despite the code looking the same. This works: ```d class Example { double[6][3] matrix; this() { matrix = 0; } } ```i see. that's a bummer - i knew the `writeln()` wouldn't work in a class body, but i assumed that because other initializations work [e.g, `int myint = 4;` or `int[69] funny = 420;`], this case would be much the same. ah well. off topic, your baba is you avatar is very cute.
Jun 11 2021
On Friday, 11 June 2021 at 08:30:29 UTC, Moth wrote:what's going on?It's a bug: [Issue 19178 - Static initialization of 2d static arrays in structs produces garbage or doesn't compile sometimes](https://issues.dlang.org/show_bug.cgi?id=19178)
Jun 11 2021