GNOME Bugzilla – Bug 694765
Can't copy generic GLib.List
Last modified: 2018-03-22 14:40:41 UTC
The following function: -------CODE---- public GLib.List<G> copy_list<G> (GLib.List<G> list) { var ret = new GLib.List<G> (); foreach (var item in list) ret.prepend (item); ret.reverse (); return ret; ----------------- gets me following errors from C compiler: /extra-data/checkout/gnome/gnome-boxes/src/util-app.vala: In function ‘_g_destroy_func0_’: /extra-data/checkout/gnome/gnome-boxes/src/util-app.vala:397:28: error: ‘g_destroy_func’ undeclared (first use in this function) /extra-data/checkout/gnome/gnome-boxes/src/util-app.vala:397:28: note: each undeclared identifier is reported only once for each function it appears in Had a chat on IRC about this: <Lethalman> interesting <Lethalman> seems to not happen with gee <flo> probably about simple generics <Lethalman> indeed <Lethalman> flo, his function is not simple generics, but glib.list is... there must be something wrong in the compiler <Lethalman> zeenix, it's vala bug obviously <flo> yes, that's what I thought <zeenix> ah ok * zeenix files <Lethalman> and... I don't see a workaround <zeenix> i guess i can make it specific to GLib.Object <Lethalman> either that or write it in C <Lethalman> zeenix, there's a copy_deep in GList <Lethalman> but I have no idea how to pass the CopyFunc in vala, I believe we need some compiler support
I thought I'll make the function act on GLib.Object as a workaround but that also gets me into trouble it seems :( -----CODE--------------- public GLib.List<GLib.Object> copy_list (GLib.List<GLib.Object> list) { var ret = new GLib.List<GLib.Object> (); foreach (var item in list) ret.prepend (item); ret.reverse (); return ret; } ... private GLib.List<DeviceDriver> get_drivers (DriverTestFunction test_func) { var drivers = new GLib.HashTable<string,DeviceDriver> (str_hash, str_equal); ... // We can't just return drivers.get_values () as we don't own the list returned by this call return copy_list (drivers.get_values ()) as GLib.List<Osinfo.DeviceDriver>; ------------------- I get the following errors/warnings: unattended-installer.vala:792.16-792.82: error: Operation not supported for this type return copy_list (drivers.get_values ()) as GLib.List<Osinfo.DeviceDriver>; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ** (valac:2718): CRITICAL **: vala_ccode_base_module_store_temp_value: assertion `initializer != NULL' failed ** (valac:2718): CRITICAL **: vala_ccode_function_add_assignment: assertion `right != NULL' failed
Don't abuse "as", use a cast.
(In reply to comment #2) > Don't abuse "as", use a cast. How is this an abuse?
Created attachment 369906 [details] [review] codegen: Free generic elements of glib collections It needs to be possible to use parameters or fields/properties which hold dup/free functions for a generic type in scope. This required to make the destroy_func being a parameter with the benefit of being able to use g_*_free_all directly and adding a _g_node_free_all wrapper with a compatible signature.
Attachment 369906 [details] pushed as e0440ce - codegen: Free generic elements of glib collections