Denis Shipilov
Denis Shipilov

Reputation: 53

GLib.Variant new empty array

Documentation says that child_type must be non-None if n_children is zero. So I can't get it. Where is the logic here? What type should I use for an empty array? Here is my example code:

from gi.repository import GLib
child_type = None
child_value = None
result_value = GLib.Variant.new_array(child_type, child_value)

An here is the output:

./test.py:5: Warning: g_variant_new_array: assertion 'n_children > 0 || child_type != NULL' failed
  result_value = GLib.Variant.new_array(child_type, child_value)
Traceback (most recent call last):
  File "./test.py", line 5, in <module>
    result_value = GLib.Variant.new_array(child_type, child_value)
TypeError: constructor returned NULL

When I create non-empty array - everything works fine. I can define child_type by type of child items. But with an empty array I stuck.

Have anyone any idea?

P.S. child_value = [] doesn't work

Upvotes: 0

Views: 338

Answers (1)

Philip Withnall
Philip Withnall

Reputation: 5733

What type should I use for an empty array?

The same type you’d use for a non-empty array. GVariant arrays are strongly typed, so an empty array of type as is semantically different from an empty array of type au. You need to specify the type of the elements which would be in the array were it not empty.

(More technically, the type of the array element actually makes a difference to the alignment requirements of the serialised form of the array, even if the array is empty. So it is a practical difference as well as a purely semantic one.)

Upvotes: 1

Related Questions