Re: Function completion for GVariant maybe types?

On 20/02/13 09:45, Markus Elfring wrote:
Do any software developers dare to think about extensions around the function
"g_variant_new_maybe" once more?

Ryan and I have explained several times why we don't think the semantics
you requested are useful to have in GLib. Repeatedly asking for the same
functions, without providing any more information, is not going to get
these functions added.

Repeating Ryan's question from before:
Can you please explain, at a higher level,
what you are trying to accomplish?

"I want to reset a variant variable" is not the explanation he asked
for. The explanation we want might look more like "I want to use
variants as values in a database, where 'maybe x' is a possible column
type". Please explain. When you do, we will hopefully be able to either
see why the functions and semantics you suggested are useful, or suggest
a way you can achieve your high-level goal without them.

If you want to use these functions in a particular module but you are
not willing to explain why you need them, then they are not going to be
added to GLib. Add them to that other module instead, or reconsider its
design so you don't need them.


Returning to your original mail and making one last attempt to guess
what it is that you want:

It is important to distinguish between a variable that can point to a
variant object (a variable of type GVariant *), and the variant object
that it can point to.

Variables of type GVariant * are mutable (can be set to point to a
GVariant) and nullable (can be set to NULL). If what you want is a
variable that contains "maybe x", the simplest way is to have a GVariant
* variable whose value is either a variant object of type x, or NULL.

Variant objects are immutable, and have a particular type and value. You
cannot change the type or value of a variant object, at all. What you
*can* do is to change the value of a GVariant * variable, so it points
to a *different* variant object.

You only need to get into 'maybe' types if you have a container (e.g.
serialized structure or D-Bus message) containing 'maybe x', in which
case the immutability of variant objects makes your proposed semantics
not very useful.


I don't think this is worthwhile. If g_variant_is_maybe (GVariant *v)
existed, it'd be exactly equivalent to:

    g_variant_is_of_type (v, G_VARIANT_TYPE_MAYBE)

which is just as easy to write.


This is not meaningful. A GVariant object (the thing pointed to by a
GVariant *) has a definite, immutable value: either it is Nothing, or
Just x (for some x). GVariant objects are value-objects, not "slots" for
value objects.

A variant v is a maybe containing Nothing if and only if this expression
is true:

    (g_variant_is_of_type (v, G_VARIANT_TYPE_MAYBE) &&
     g_variant_get_maybe (v) == NULL)

If you are talking about a GVariant * variable (pointer to variant
object) and you want a signature more like

    g_variant_location_can_be_null (GVariant **variable)

then, no, that's impossible. GLib can't know what the semantics of your
variable are: only you (and your code's documentation) can know that.

Or, if you want the function's parameter to be a GVariantType rather
than a GVariant, then this is just g_variant_type_is_maybe (type).

I see a need for a alternative to the interface
"g_variant_new_maybe". I would like to replace a maybe instance
directly instead of allocating a new one.

You can't "replace a maybe instance". Variant objects are immutable:
creating a new variant object with a different value is always a new

It is possible to reassign a GVariant * variable to point to a different
variant object, but that doesn't need a special function: just write the

4. g_variant_set_to_nothing/none
  How should a GVariant variable be reset to the special marker
"nothing" after
it was used with other concrete values for a while?

Rather than messing about with "maybe x" types, I think you would find
it much easier to have this structure:

    /* Contains a variant of type x, or NULL.
     * (transfer full) */
    GVariant *my_variant = NULL;


    /* set the variable to a non-NULL value */
    if (my_variant != NULL)
      g_variant_unref (my_variant);

    my_variant = g_variant_ref_sink (g_variant_new_int64 (y));


    /* set the variable to a NULL value */
    if (my_variant != NULL)
      g_variant_unref (my_variant);

    my_variant = NULL;

and if you need to serialize it somewhere, *then and only then* wrap its
current value in an immutable "maybe x" variant object:

    g_dbus_message_set_body (message,
        ..., g_variant_new_maybe (G_VARIANT_TYPE_INT64, my_variant),

(For "x" and "int64" substitute whatever type the thing has, which you
should know already - either hard-coded, or by reading a GVariantType *
variable from your database schema or whatever - because you can't do
anything useful with data of an unknown type.)

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