在Vala中继承接口 - 与base方法不兼容

时间:2016-12-21 13:26:59

标签: c gtk vala gobject

我试图在Vala中实现Gtk.StyleProvider。 "基类" (在C中)看起来像:

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

并在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);
}

根据NULL的文档,前两种方法只应返回GtkStyleProvider

因此,我写了一些像这样的Vala:

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
    }
}

我对前两种方法有疑问。如果我把它们写在这里(带有?),那么我会收到以下错误:

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)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

gtk_style_provider_get_style()方法是相同的。

如果我删除了?,每种方法会出现以下两个错误:

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;
                ^^^^^^^^^^^

第一个错误尤其对我来说有点奇怪,因为结果是"错误:预期类型,得到TYPE"!

在前两种方法中添加unowned仍然会导致类似的错误。

我应该如何在Vala中实现Gtk.StyleProvider接口?

1 个答案:

答案 0 :(得分:1)

这在我的系统上编译没有错误或警告(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
    }
}

我做了这些改变:

  • 除了界面外,还来自GLib.Object
  • 在第一种方法上使用unowned
  • 从返回类型中删除nullable。
  • 将null转换为实际的类类型。 (这不是很好,但问题在于vapi文件。)
  • 为out参数指定一个虚拟值,使编译警告免费;)