Inheriting interface in Vala - incompatible with base method

269 views Asked by At

I'm trying to implement a Gtk.StyleProvider in Vala. The "base class" (in C) looks like:

GtkIconFactory *        gtk_style_provider_get_icon_factory ()
GtkStyleProperties *    gtk_style_provider_get_style ()
gboolean                gtk_style_provider_get_style_property ()

and in VAPI:

[CCode (cheader_filename = "gtk/gtk.h")]
public interface StyleProvider {
    public abstract unowned Gtk.IconFactory get_icon_factory (Gtk.WidgetPath path);
    public abstract unowned Gtk.StyleProperties get_style (Gtk.WidgetPath path);
    public abstract bool get_style_property (Gtk.WidgetPath path, Gtk.StateFlags state, GLib.ParamSpec pspec, GLib.Value value);
}

Where the first two methods should only return NULL according to the documentation for GtkStyleProvider.

Thus, I wrote some Vala like this:

public class DerivedStyleProvider : Gtk.StyleProvider
{
    public Gtk.IconFactory? get_icon_factory (Gtk.WidgetPath path)
    {
        return null;
    }

    public Gtk.StyleProperties? get_style (Gtk.WidgetPath path)
    {
        return null;
    }

    bool get_style_property (Gtk.WidgetPath path,
                    Gtk.StateFlags state,
                    GLib.ParamSpec pspec,
                    out GLib.Value value)
    {
        return false; //TODO
    }
}

I have a problem with the first two methods. If I have them as written here (with a ?), then I get the following error:

error: overriding method `DerivedStyleProvider.get_icon_factory' is incompatible 
with base method `Gtk.StyleProvider.get_icon_factory': Base method expected 
return type `Gtk.IconFactory', but `Gtk.IconFactory?' was provided.
    public Gtk.IconFactory? get_icon_factory (Gtk.WidgetPath path)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The gtk_style_provider_get_style() method is the same.

If I remove the ?, I get the following two errors per method:

error: overriding method `DerivedsStyleProvider.get_icon_factory' 
is incompatible with base method `Gtk.StyleProvider.get_icon_factory': Base 
method expected return type `Gtk.IconFactory', but `Gtk.IconFactory' was provided.
        public Gtk.IconFactory get_icon_factory (Gtk.WidgetPath path)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
src/Preferences.vala:138.3-138.14: warning: `null' incompatible with 
return type `Gtk.IconFactory`
                return null;
                ^^^^^^^^^^^

The first error especially is a bit strange to me, as the upshot is "error: expected TYPE, got TYPE"!

Adding unowned to the first two methods still results in similar errors.

How should I implement a Gtk.StyleProvider interface in Vala?

1

There are 1 answers

3
Jens Mühlenhoff On BEST ANSWER

This compiles without errors or warnings on my system (Vala 0.32.1):

public class DerivedStyleProvider : GLib.Object, Gtk.StyleProvider
{
    public unowned Gtk.IconFactory get_icon_factory (Gtk.WidgetPath path)
    {
        // Evil cast to work around buggy declaration in VAPI file
        return (Gtk.IconFactory) null;
    }

    public Gtk.StyleProperties get_style (Gtk.WidgetPath path)
    {
        // Evil cast to work around buggy declaration in VAPI file
        return (Gtk.StyleProperties) null;
    }

    bool get_style_property (Gtk.WidgetPath path,
                    Gtk.StateFlags state,
                    GLib.ParamSpec pspec,
                    out GLib.Value value)
    {
        // I just assigned something here to make the compiler happy, you should make sure to use a correct value
        value = Value (typeof (string));
        return false; //TODO
    }
}

I made these changes:

  • Derive from GLib.Object in addition to the interface.
  • Use unowned on the first method.
  • Remove the nullable from the return types.
  • Cast null into the actual class types. (Which is not pretty, but the problem is with the vapi file.)
  • Assign a dummy value to the out parameter to make compiling warning free ;)