GNOME Bugzilla – Bug 647788
Vala doesn't allow making fixed-sized arrays from constants
Last modified: 2018-05-22 14:01:12 UTC
Created attachment 185964 [details] Test.vala Vala doesn't allow making fixed-sized arrays from constants. This is absolutely necessary since there are no preprocessor directives in Vala and most C libraries define symbolic names for these things. The attached test program demonstrates the problem: $ valac-0.12 Test.vala && ./Test Test.vala:4.13-4.19: error: syntax error, expected `]' or integer literal int array[MAXSIZE]; ^^^^^^^ Compilation failed: 1 error(s), 0 warning(s) $
This should work as : class Test : Object { const int MAXSIZE = 1024; public static void main(){ var array = new int[MAXSIZE]; stdout.printf("array has %d elements\n", array.length); } }
(In reply to comment #1) > This should work as : > > class Test : Object { > const int MAXSIZE = 1024; > public static void main(){ > var array = new int[MAXSIZE]; > stdout.printf("array has %d elements\n", array.length); > } > } Yes, but this isn't what I want. Your example creates a variable-size array which is allocated on the heap. I want a fixed-size array allocated on the stack (if used as a local variable) or in-line allocated (if used as a field in a struct or a class). See also: https://live.gnome.org/Vala/Tutorial#Arrays I will attach a new example which may make this more clear. A real world example of the problem can be found here: http://cgit.freedesktop.org/pulseaudio/pulseaudio/tree/vala/libpulse.vapi?id=97da92d894c2a29bc3ccf60dc904e4099b30f47e#n378 Best regards Alexander Kurtz
Created attachment 247649 [details] Demo.vala
This problem still exists in Vala 0.20: $ cat Demo.vala const int LENGTH = 32; public struct Demo { int array[LENGTH]; } public static int main(string[] argv){ Demo demo = Demo(); stdout.printf("Array length is %d\n", demo.array.length); return 0; } $ valac --version Vala 0.20.1 $ valac Demo.vala Demo.vala:4.12-4.17: error: syntax error, expected `]' or integer literal int array[LENGTH]; ^^^^^^ Compilation failed: 1 error(s), 0 warning(s) $ Best regards Alexander Kurtz
Using Vala 0.30.1 this is still broken :(
-- 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/194.