[Vala] Variable-length array in struct and initializing an array of such structs



Hi,

If I understand correctly, arrays declared like this:
 type[] an_array
are arrays with a variable length. This gets translated into C as a
pointer to a dynamically allocated array, with some extra fixed-size
variables like length and size. Is this correct?

Taking that in mind, I drew the conclusion that such variable-length
arrays could be members of a struct, as the parts actually ending up in
the C-struct are fixed-size.

In Vala, I defined this struct:
 struct Test {
        double some_double;
        uint[] array;
 }

Then I tried using it in the following manner:
 const Test[] tests = {
        { 2.5, {1, 2, 3} },
        { 3.5, {2, 3} }
 };

 int main { return 0; }

This resulted in the following failure (valac):
 ERROR:valaccodearraymodule.c:1108:vala_ccode_array_module_real_get_array_length_cvalue: assertion failed: 
(size != null && size.size >= dim)

Then I tried the following:
 int main() {
        Test[] tests = {
                Test() { some_double = 2.5, array = {1, 2, 3} },
                Test() { some_double = 2.7, array = {2, 3} }
        };

        return 0;
 }

Which did compile.

I attached two files with complete code of the two examples I posted. I
compiled them using 'valac test.vala' (version 0.29.2)

Are the code examples valid Vala code? I'm in doubt because the
compiler fails instead of giving a warning (example 1).

Thanks for your time,
Steef

P.S.
For those wondering why on earth I'd be interested in this: I'm writing
a small application with an interface that consists of functions bound
to a sequence of pressed keys. Ideally, I'd like to define those
bindings like this:
 const Binding[] bindings = {
        { function1, {key1, key2} },
        { function2, {key3} },
        ...
 };

Attachment: example_1.vala
Description: Text document

Attachment: example_2.vala
Description: Text document



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]