g_variant_equal() and dictionaries

For anyone using g_variant_equal() with a type containing a dictionary, you should the aware that, somewhat unexpectedly in my opinion, g_variant_equal() only returns true if (and only if) the keys in the dictionary are in the same order (GVariant implements dictionaries as an array of key-value pairs).

You can resolve this by using a function that recursively checks for semantic equivalence (such as this one).

I’m sure I’m not going to be the only person to find this useful. I’ve filed this as bug #622590.

Author: Danielle

Danielle is an Australian software engineer, computer scientist and feminist. She doesn't really work on GNOME any more (sadly). Opinions and writing are solely her own and so not represent her employer, the GNOME Foundation, or anyone else but herself.

2 thoughts on “g_variant_equal() and dictionaries”

  1. Did Ryan elaborate on why the order is important? Because this sounds like a bug in g_variant_equal() to me. And being able to memcmp() two variants does not sound like a good enough excuse to me.

  2. Nothing that seemed really convincing to me. The main use for g_variant_equal() seems to be GHashTables, but you can only use simple variants (i.e. of type ‘?’) with g_variant_hash() anyway.

    This kind of makes sense if you’re expanding a dictionary variant (i.e. of type a{?*}) into a GHashTable (which I think should also be a method provided by GLib), but in the more generic comparison case, there should be a method call for that.

Leave a Reply

Your email address will not be published. Required fields are marked *

Creative Commons Attribution-ShareAlike 2.5 Australia
This work by Danielle Madeley is licensed under a Creative Commons Attribution-ShareAlike 2.5 Australia.