After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 548428 - string[][] and foreach
string[][] and foreach
Status: RESOLVED FIXED
Product: vala
Classification: Core
Component: Arrays
0.3.x
Other Linux
: Normal normal
: ---
Assigned To: Vala maintainers
Vala maintainers
Depends on:
Blocks:
 
 
Reported: 2008-08-19 12:13 UTC by Philip Van Hoof
Modified: 2010-10-18 09:38 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description Philip Van Hoof 2008-08-19 12:13:41 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) {
   }
}
Comment 1 Chris Lord 2008-08-21 16:36:56 UTC
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.
Comment 2 Simon Wenner 2010-04-04 13:05:41 UTC
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
Comment 3 Anatol Pomozov 2010-10-02 01:05:24 UTC
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)
Comment 4 Jürg Billeter 2010-10-16 13:37:35 UTC
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.
Comment 5 Chris Lord 2010-10-18 09:14:43 UTC
I wouldn't really call this 'fixed' as such... Is there a bug I can subscribe to for stacked array support?
Comment 6 Jürg Billeter 2010-10-18 09:38:48 UTC
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.