Re: [Vala] Sorting a GLib.List<string>



Didier 'Ptitjes' wrote:
Hi Magnus,

Magnus Therning wrote:
What function should I use to sort a list of strings to avoid the compiler warning I get with strcmp:

  ...
  my_list.sort( strcmp );
  ...

Test.vala:60: warning: passing argument 2 of ‘g_list_sort’ from incompatible pointer type /usr/include/glib-2.0/glib/glist.h:101: note: expected ‘GCompareFunc’ but argument is of type ‘int (*)(const char *, const char *)’

You have to explicitly cast it to CompareFunc. Indeed, the GLib.strcmp is just a convenience binding around stdlib's strcmp, and they appear to not have the exact same signatures. IIRC those are:

int strcmp (void* a, void* b)
gint GCompareFunc (gconstpointer, gconstpointer)

That'll have to do for now then.  Though I think I agree with the suggestion
of doing the cast in "Vala's C" (whatever that means :-).

However, I would suggest you to use libgee-0.5 which is expected to be (finally) released on Monday September 29, 2009 (or master from git).

Yes, I'll switch to using Gee 0.5 once it's released.

However, that option isn't always desirable, e.g. if external APIs require
GLib.List and one doesn't want to pay the price of conversion back and forth.
It'd still be worth hiding the cast from the Vala programmer.

Gee.List<G>.sort uses an efficient TimSort algorithm, and for free you get:
* transparent uses of standard compare funcs for primitive, string and object types
 * possibility to make your classes inherit Comparable<G>
* rid of equal functions at List construction time for primitive, string and object types

I'm eagerly awaiting Gee 0.5 already... you really don't need to do any more
sales pitching ;-)

/M

--
Magnus Therning                        (OpenPGP: 0xAB4DFBA4)
magnus@therning.org          Jabber: magnus@therning.org
http://therning.org/magnus         identi.ca|twitter: magthe

Attachment: signature.asc
Description: OpenPGP digital signature



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