g_object_class_override_property_default_value()
Submitted by Benjamin Otte (Company)
Link to original bug (#609653)
Description
So, I've been thinking about this API for a while. It's related to bug 557151 and bug 587256.
The goal from those 2 bugs: Provide a way for subclasses to override default values for properties.
The use cases:
- Make g_object_new() default to the desired value for (CONSTRUCT?) properties without a need to specify the property in g_object_new()'s arguments, in particular getting it right for subclasses, too.
- Make g_object_class_list_properties() hand you the correct default value. So it's basically ease and correctness of instantiation and introspection.
Current workarounds for this missing functionality in GObject are:
- Overriding the desired value directly in the instance init function, like calling gtk_widget_set_app_paintable (widget, TRUE); Only works for non-construct properties and violates (2) above.
- Overriding the desired value in the instance init function with g_object_set(). Same problem as above, plus the problems in bug 557151.
- Overriding the value in class->constructed or passing one's own constructor. Works for all types of properties, but requires intimate knowledge of GObject. It's also very ugly. And it violates (2).
- Providing a custom _new() function that correctly sets the desired properties. This is the preferred method currently (gtk does that), but it violates both (1) and (2).
So I've been thinking about adding a subclass of / extending GParamSpecOverride to allow overriding the deault value. Unfortunately GParamSpecOverride was meant for overriding interface properties and is therefor treated like an implementation detail and rarely exposed. So it doesn't fit the job very well. So I'm out of obvious solutions.
And before I'm going to write any patches, there's some systemic issues I need a solution to:
- Should you be allowed to override defaults for non-construct properties? How would that work? Would it make the property a construct property?
- Would g_object_class_list_properties() return the override property? It's the only one that gives you the default value, but it's not the one that knows about constraints (like min/max value for example). GParamSpecOverride isn't returned for example.
- How far do I want to go? Do I aim at making gtk_foo_new() equivalent to g_object_new(GTK_TYPE_FOO, NULL) and use overrides where possible or do I just want to solve the problems in the bugs above?
Input appreciated.