GNOME Bugzilla – Bug 663251
x[i]++ doesn't work with GenericArray
Last modified: 2018-05-22 14:12:10 UTC
[rainct, tmp]$ cat foo.vala public void main(string[] args) { var arr = new GenericArray<int> (); arr.length = 3; arr[0] = 1; arr[1] = 2; arr[2] = 3; arr[1]++; } [rainct, tmp]$ valac foo.vala foo.vala:9.5-9.10: error: The expression `GLib.GenericArray<int>' does not denote an array arr[1]++; ^^^^^^ Compilation failed: 1 error(s), 0 warning(s)
Furthermore, the syntax "arr[x] += 1" compiles but results in an incorrect value.
*** Bug 662198 has been marked as a duplicate of this bug. ***
GenericArray is a binding to GLib's GPtrArray (https://developer.gnome.org/glib/stable/glib-Pointer-Arrays.html). It is an array of pointers to any data type. Vala resrticts the data type to that given at construction time by the type parameter using SimpleGenerics. For the example above it is int. It doesn't make sense to increment an item in the array because that is a pointer and not a value. So the 'does not denote an array' error makes some sense. Ideally GPtrArray would have a replace function, but updating can be done with dereferencing the pointer and updating the value or using remove_index() and insert(). Also int is a value type, not a reference type(pointer), so it should be boxed for GenericArray.
-- GitLab Migration Automatic Message -- This bug has been migrated to GNOME's GitLab instance and has been closed from further activity. You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/vala/issues/243.