如何在Haskell中正确调用GI.Gtk scrolledWindowNew?

时间:2018-01-31 22:22:43

标签: haskell gtk3

documentation说:

  

通常你想传递Nothing进行调整

scrolled <- scrolledWindowNew Nothing Nothing

给我以下错误,可能是由错误导入引起的。

• Overlapping instances for GI.Gtk.Objects.Adjustment.IsAdjustment
                              a0
    arising from a use of ‘scrolledWindowNew’   Matching instances:
    instance [overlappable] (Data.GI.Base.BasicTypes.GObject a,
                             Data.GI.Base.Overloading.UnknownAncestorError
                               GI.Gtk.Objects.Adjustment.Adjustment a) =>
                            GI.Gtk.Objects.Adjustment.IsAdjustment a
      -- Defined in ‘GI.Gtk.Objects.Adjustment’
    ...plus one instance involving out-of-scope types
    (use -fprint-potential-instances to see them all)

我的导入看起来像这样

import qualified GI.Gtk as GI (init,
                               main)
import GI.Gtk (mainQuit,
               onWidgetDestroy,
               windowNew,
               widgetShowAll,
               containerSetBorderWidth,
               headerBarNew, headerBarSetTitle, headerBarSetSubtitle,headerBarSetShowCloseButton,
               scrolledWindowNew
               )

import GI.Gtk.Objects.Window
import GI.Gtk.Enums

可能的解决方案

文档中是否有错误?使用noAdjustment而不是Nothing似乎有效。

import qualified GI.Gtk as GI (init,
                               main)
import GI.Gtk (mainQuit,
               onWidgetDestroy,
               windowNew,
               widgetShowAll,
               containerSetBorderWidth,
               headerBarNew, headerBarSetTitle, headerBarSetSubtitle,headerBarSetShowCloseButton,
               scrolledWindowNew, scrolledWindowSetPolicy
               )

import GI.Gtk.Objects.Window
import GI.Gtk.Objects.Adjustment (noAdjustment)

import GI.Gtk.Enums (WindowType(..), PolicyType(..))



  scrolled <- scrolledWindowNew noAdjustment noAdjustment
  scrolledWindowSetPolicy scrolled PolicyTypeNever PolicyTypeAutomatic

Leksah源代码中的灵感。

请发表评论

我试图将Haskell转换为这里的Python示例: http://python-gtk-3-tutorial.readthedocs.io/en/latest/layout.html#flowbox

解决问题的工作版可以在这里找到: https://github.com/bigos/my-haskell-gtk-3-tutorial/blob/master/5-layout-containers/6-flow-box.org

1 个答案:

答案 0 :(得分:3)

如果您查看数据GI.Gtk.Objects.ScrolledWindow.scrolledWindowNew的签名:

scrolledWindowNew :: (HasCallStack, MonadIO m, IsAdjustment a, IsAdjustment b)  =>
                     Maybe a ->
                     Maybe b ->
                     m ScrolledWindow

如果你打电话就像你做的那样

scrolled <- scrolledWindowNew Nothing Nothing

编译器知道关于(例如)第一个参数的两件事。它知道的类型为Maybe a,约为a,它是IsAdjustment的实例类型。

但是编译器需要确切地知道类型a是什么,错误消息是由于它无法推断出它。 (可悲的是,它非常奇怪而且不是很有帮助,因为那些重叠的实例是介入的。)

让我们来看看为什么noAdjustment有效。如果我们查看它的文档GI.Gtk.Objects.Adjustment.noAdjustment,则其类型签名为:

noAdjustment :: Maybe Adjustment

和文档说明:

  

Nothing :: Maybe Adjustment的便利别名。

Adjustment是一种具体类型,碰巧有IsAdjustment的实例。

所以编译器很高兴,因为它现在知道参数的确切类型。

另见

What is the type of Nothing in Haskell?

Using Haskell's "Maybe", type declarations [beginner's question]