GNOME Bugzilla – Bug 548428
string[][] and foreach
Last modified: 2010-10-18 09:38:48 UTC
This doesn't work (you get a parse error): string [][] stuff = .. foreach (string [] strings in stuff) { foreach (string s in strings) { } } This does work: string [][] stuff = .. uint x; for (x = 0; x < stuff.length; x++) { foreach (string s in stuff[x]) { } } This also does work: string [][] stuff = ... foreach (weak string [] strings in stuff) { foreach (string s in strings) { } }
This applies to arrays of objects too. Also, the length of the inner arrays will always be returned as -1, so although the latter two examples compile, the inner array will not be iterated over.
Vala 0.8.0 is still affected by this bug, but the first testcase no longer gives you a parse error but critical messages: ** (valac:16076): CRITICAL **: vala_ccode_array_module_real_get_array_length_cexpression: assertion `array_expr != NULL' failed ** (valac:16076): CRITICAL **: vala_ccode_function_call_add_argument: assertion `expr != NULL' failed
In vala 0.10.0 following example causes compie error int[,] stuff = { {2, 4, 6, 8}, {3, 5, 7, 9}, {1, 3, 5, 7} }; void main() { foreach (int[] ints in stuff) { foreach (int i in ints) { stdout.printf(i); } } } test.vala:8.3-8.31: error: Foreach: Cannot convert from `int' to `int[]' foreach (int[] ints in stuff) { ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Compilation failed: 1 error(s), 0 warning(s)
commit cc435dd6ec069b87e986fa454e80e5fe80a0df72 Author: Jürg Billeter <j@bitron.ch> Date: Sat Oct 16 15:17:23 2010 +0200 Report error when trying to use stacked arrays Stacked array support was never completed. Report an error instead of generating incorrect C code. Fixes bug 546603, bug 548428, bug 548429, bug 565865, bug 565872, bug 571322, bug 572649, and bug 576611.
I wouldn't really call this 'fixed' as such... Is there a bug I can subscribe to for stacked array support?
There is no resolution in bugzilla that really fits. It's an unsupported feature and it was a bug that it didn't report a proper error. That bug is fixed. There is no open enhancement request to support stacked arrays at the moment as I'm not aware of a sane and common C API/ABI for the general case. Most people requesting stacked array support needed it for D-Bus. For that case, I recommend to use GLib.Variant instead, which can handle stacked arrays just fine. For non-DBus cases, I recommend to use GLib.GenericArray, GLib.Array, or Gee.List, they should all support stacking. If you have a suggestion how stacked arrays could be implemented with sane length handling for inner arrays, I'd consider implementing it.