GNOME Bugzilla – Bug 741491
Const struct array fields cannot be initialized
Last modified: 2018-05-22 15:20:45 UTC
Maybe i'm doing something wrong, but i can't find any way to do this in Vala. This is the test: ``` struct Foo { public string name; public string title; public int[] ages; } const Foo foo = { name: "Hello", title: "World", ages: { 1,2,3 } }; void main() { print ("%s\n", foo.ages[1]); } ```
Created attachment 308080 [details] Example of failing code Results in following compiler error: ERROR:valaccodearraymodule.c:1108:vala_ccode_array_module_real_get_array_length_cvalue: assertion failed: (size != null && size.size >= dim)
To further specify what this is about: the compiler currently fails when attempting to initialize an array in a const struct inline. Relevant discussion: https://mail.gnome.org/archives/vala-list/2015-July/msg00010.html Like Al Thomas mentioned, it seems like the compiler could either give a warning or the inline initialization could be implemented. I'm not exactly sure whether this is considered valid Vala code in the first place though. Sorry for the double post
Vala should emit an error, because that array is supposed to be heap-allocated, hence it's not possible in a const context. In a const context you should explictly tell vala (and C) that array is an inline array of 3 elements, like int ages[3];
*** Bug 597817 has been marked as a duplicate of this bug. ***
*** Bug 670248 has been marked as a duplicate of this bug. ***
-- 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/486.