GNOME Bugzilla – Bug 641267
ValaCCodeArrayModule, support for zero/negative slice values in arrays.
Last modified: 2018-05-22 13:53:09 UTC
Created attachment 179905 [details] [review] valaccode: ArrayModule, support for negative slice values in arrays. This patch allows to perform slices on arrays using negative indexes, as supported by string slice function. I.e.: int[] a = { 42, 23, 11 }; int[] b = a[-2:0]; //23, 11 int[] c = a[-2:-1]; // 23 This implementation doesn't shows errors when the slice values aren't valid, in some cases (with too great negative inputs) it just don't slice the array, also if I think that would be better to add more checks in the generated code (maybe asserts when no IntegerLiteral's are used) when start/stop slice indexes aren't well formed. Comments about this are welcome! It also add the support for zero-end slice value, so int[] b = a[1:0] // creates an array with all the values but the first int[] b = a[-1:0] // creates an array with just the latest value
Hey, I do not know if this will be fixed with the above patch or if it is a different problem. The following snippet does not work: void main() { string[] arr = {"Hello", "World", "!"}; debug (string.joinv(" ", arr[0:1])); // Does not work as expected string[] arr2 = arr[0:1]; debug (string.joinv(" ", arr2)); // Works as expected }
Mh, I guess this is another issue; with the present patch I still get this output: ** (process:13066): DEBUG: array.vala:14: Hello World ! ** (process:13066): DEBUG: array.vala:16: Hello
With Vala 0.38, I get ** Message: Untitled 2.vala:4: Hello ** Message: Untitled 2.vala:6: Hello for your snippet, which I guess is expected. There is still something broken in the negative slicing, i.e. I can't find any invocation that reliably gets me an array consisting of the last element only.
-- 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/162.