c - 在 Vala 中继承接口(interface) - 与基本方法不兼容

标签 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);
}

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

因此,我写了一些像这样的 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;
                ^^^^^^^^^^^

特别是第一个错误对我来说有点奇怪,因为结果是“错误:期望的类型,得到的类型”!

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

我应该如何在 Vala 中实现一个 Gtk.StyleProvider 接口(interface)?

最佳答案

这在我的系统(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
    }
}

我做了这些改变:

  • 除接口(interface)外,还派生自 GLib.Object
  • 在第一个方法上使用unowned
  • 从返回类型中移除可空值。
  • 将 null 转换为实际的类类型。 (这不是很漂亮,但问题出在 vapi 文件上。)
  • 为 out 参数分配一个虚拟值以消除编译警告;)

关于c - 在 Vala 中继承接口(interface) - 与基本方法不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41263888/

相关文章:

c - 如何从内存中正确分配结构

无法读取文件并获取结果

python - 在 pygtk 中处理双击和单击事件

vala - vala 中的 GtkColorChooser 对话框

c# - 如何在不同的机器上调用dll方法?

c - 在 Linux 中 "which source"什么都不返回?

c - cmake 中 gcc 的 -mwindows 选项的等效项是什么?

c - gtk 中无边框的 MessageDialog

vala - 没有实例方法或闭包的目标无法创建委托(delegate)是什么意思

c - 瓦拉内存管理