Re: [Vala] compiler warning for Variant.strv()



    > On Thursday, 25 January 2018, 10:32:07 GMT, Yasushi SHOJI <yasushi shoji gmail com> wrote:  
void main () {
        Variant ary = new Variant.strv({"foo", "bar", "baz"});
}


In file included from /usr/include/glib-2.0/glib/gmessages.h:35:0,
                from /usr/include/glib-2.0/glib.h:62,
                from /tmp/a.vala.c:5:
/usr/include/glib-2.0/glib/gvariant.h:118:33: note: expected ‘const
gchar * const* {aka const char * const*}’ but argument is of type
‘gchar ** {aka char **}’
GVariant *                      g_variant_new_strv
  (const gchar * const  *strv,

You can use Vala's implicit conversion module to create a string array.I've figured out how to do this for 
string arrays, not just basic types. Itdoesn't produce the C warnings when compiling:
void main () {    string[] my_array = {"foo", "bar", "baz"};
    test (my_array);
    Variant my_variant = my_array;
    test (my_variant);
 }

void test (Variant variant) {
    print (@"$(variant.get_type_string ())\n");
    print (@"$(variant.print (true))\n");
}

The implicit conversion is invoked when assigning the array to a variable of type Variant. It is also invoked 
when passing the string array as an argumentto a function that expects a Variant type parameter.
This also works for GLib HashTable, which is a dictionary over D-Bus. This should be very useful for using 
DBusConnection. Code snippet:
var a = new HashTable of string, Variant ( str_hash, str_equal );
a.insert( "first", 1 );
a.insert( "second", "test" );
b:Variant = a;

A couple of references for anyone interested:https://wiki.gnome.org/Projects/Vala/DBusServerSample#Type_Table
https://valadoc.org/gio-2.0/GLib.DBusConnection.html

All the best,
Al

  


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