Re: [Vala] initializing structures



Looking at the Vala test code, I realized the syntax should be:

using GLib;

public struct mystruct {
    public string test1;
    public int test2;
}

void main (string[] args)
{

    /* the following code works fine */
    mystruct my_b = {"this is a test", 10};
    debug("%s, %i", my_b.test1, my_b.test2);

    /* now works */
    mystruct[] my_a = {
        mystruct() {test1 = "this is a test", test2 = 10},
        mystruct() {test1 = "this is a test"},
        mystruct() {test2 = 12}
    };

    debug("%s, %i", my_a[0].test1, my_a[0].test2);
    debug("%s, %i", my_a[1].test1, my_a[1].test2);
    debug("%s, %i", my_a[2].test1, my_a[2].test2);
}


returns:
** (process:18336): DEBUG: test_struct.vala:15: this is a test, 10
** (process:18336): DEBUG: test_struct.vala:24: this is a test, 10
** (process:18336): DEBUG: test_struct.vala:25: this is a test, 0
** (process:18336): DEBUG: test_struct.vala:26: (null), 12


On Wed, Sep 3, 2008 at 4:14 PM, Cliff Brake <cliff brake gmail com> wrote:
Just an observation -- not sure what is supposed to work or not ....

using GLib;

public struct mystruct {
   public string test1;
   public int test2;
}

void main (string[] args)
{

   /* the following code works fine */
   mystruct my_b = {"this is a test", 10};
   debug("%s, %i", my_b.test1, my_b.test2);

   /* does not compile */
   mystruct[] my_a = {
       {"this is a test", 10};
   };

   /* works */
   mystruct[] my_a = new mystruct[10];

   /* the following code does not compile */
   my_a[0] = {"this is a test", 10};

   /* the following works */
   my_a[0].test1 = "this is a test";
   my_a[0].test2 = 10;

   debug("%s, %i", my_a[0].test1, my_a[0].test2);
}




--
=======================
Cliff Brake
http://bec-systems.com




-- 
=======================
Cliff Brake
http://bec-systems.com



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