Re: Function completion for GVariant maybe types?



On 20/02/13 18:11, Markus Elfring wrote:
How are you using variants?

I would like to achieve a mapping of nullable objects to indeterminate
checkboxes for example.

A GVariant with 'maybe' type seems far too complicated for this. I'd use
an integer holding a tristate enum, { NO, INDETERMINATE, YES } (or
possibly a pair of booleans, to match how GtkCheckButton works).

If you need variants for some reason, { GVariant of type 'b' with value
FALSE, NULL, GVariant of type 'b' with value TRUE } is also a perfectly
good tristate.

If, for some reason, you absolutely need non-NULL variants with type
'mb' ("maybe boolean"), then you already know that your data model is
that you have variants of that type; so you can just make one of these
objects whenever you need a new variant, and you're done:

    /* "just true" */
    yes = g_variant_ref_sink (g_variant_new_maybe (NULL,
        g_variant_new_boolean (TRUE));
    /* "just false" */
    no = g_variant_ref_sink (g_variant_new_maybe (NULL,
        g_variant_new_boolean (FALSE));
    /* "@mb nothing" */
    indeterminate = g_variant_ref_sink (g_variant_new_maybe (
        G_VARIANT_TYPE ("mb"), NULL);

There's no need to add functions to GLib for this; and in the time
you've spent arguing about it, you could have implemented all three of
those possibilities and possibly more, chosen the one you liked best,
and got on with whatever high-level problem you were solving.

Why are you using a 'maybe' type, as opposed to a GVariant * that may
be NULL?

I would like to point out that a null pointer has got a different
meaning in comparison to a specific object which was marked to contain
"nothing" (representation for an "unknown" value). Such a nullable
object can store valuable data type information, can't it?

If your data model is "my value is a variant of type 'mb'" then you
already know that you're dealing with 'mb' variants; so you don't need
to copy the data-type from an existing variant, you can just say "mb".

If your data model is "I have a variant, and I don't really know what it
means" then... you don't really know what it means, and you can't do
anything particularly useful with it until you find out.

Model-view-controller programming shouldn't be an excuse for speculative
generalization. Don't make things more complicated than they need to be;
software is usually quite complicated enough already.

    S


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