GNOME Bugzilla – Bug 569259
can't initialize top-level array
Last modified: 2014-08-23 17:54:37 UTC
When I try to compile this program: ==== test.vala === int[] a = { 1, 2, 3}; int main() { return a[0]; } === end === I get the following error: test.vala:1.1-1.7: error: Non-constant field initializers not supported in this context int[] a = { 1, 2, 3}; ^^^^^^^ But the program works fine if I either (a) declare the array to be of type const int[], or (b) omit the initializer. This is a bug, right?
This can be prevented if you place the array being initialized within a class. If it is outside of main and not in a class then one gets this message. (Vala 0.7.3)
Thanks for your report. As Chris correctly replies, your code is invalid, since in the top level context only constant initializers are supported.
It's fine if only constant initializers are supported in the top-level context, but that fact is currently undocumented: it should be mentioned in the Vala Reference Manual somewhere in the Expressions section (http://www.vala-project.org/doc/vala-draft/expressions.html) and/or in the Vala Tutorial in the "Arrays" section (http://live.gnome.org/Vala/Tutorial#head-4b9d97902aa200e69617514a45e01f014421d67e). So I'm reopening this until the documentation is fixed. If there's some other place I should be reporting documentation bugs instead then please let me know.
Still not fixed in 2013... ``` ** (valac:10077): CRITICAL **: vala_ccode_function_add_declaration: assertion `self != NULL' failed ** (valac:10077): CRITICAL **: vala_ccode_function_add_assignment: assertion `self != NULL' failed ** (valac:10077): CRITICAL **: vala_ccode_function_add_assignment: assertion `self != NULL' failed ** (valac:10077): CRITICAL **: vala_ccode_function_add_assignment: assertion `self != NULL' failed ** (valac:10077): CRITICAL **: vala_ccode_function_add_assignment: assertion `self != NULL' failed a.vala:1.1-1.7: error: Non-constant field initializers not supported in this context int[] a = { 1, 2, 3}; ^^^^^^^ Compilation failed: 1 error(s), 0 warning(s) ```
Created attachment 240317 [details] [review] Report error on non-constant field initializer in namespace Fixes bug 569259
commit 1b3b48aa7e8b550643499bf842c0ca92056aa520 Author: Simon Werbeck <simon.werbeck@gmail.com> Date: Mon Apr 1 05:00:12 2013 +0200 Error on non-const field initializer in namespace Fixes bug 569259