带有 gtk2 的 Nim 无法编译

Nim with gtk2 not compiling

我正在尝试以下从 http://rosettacode.org/wiki/Simple_windowed_application#Nim 修改的代码(该代码编译没有任何问题):

import gtk2

var
  win = windowNew WINDOW_TOPLEVEL
  label1 = labelNew  "first:"
  entry1 = entryNew
  label2 = labelNew  "second:"
  entry2 = entryNew
  button = buttonNew "Calculate"
  reslabel = labelNew  "Result"
  vbox = vboxNew(true, 1)
  counter = 0

proc clickedMe(o: var PButton, lab: PLabel) =
  inc counter
  label1.setText "You clicked me " & $counter & " times"

nim_init()
win.setTitle "My calculator"

vbox.add label1
vbox.add entry1
vbox.add label2
vbox.add entry2
vbox.add button
vbox.add reslabel

win.add vbox
discard win.signal_connect("delete-event", SignalFunc mainQuit, nil)
discard button.signal_connect("clicked", SignalFunc clickedMe, label)
win.showAll()
main()

我正在使用以下命令:

nim c -r mygui.nim 

但是,它给出了以下错误输出:

Hint: used config file '/etc/nim.cfg' [Conf]
Hint: system [Processing]
Hint: mygui [Processing]
Hint: gtk2 [Processing]
Hint: glib2 [Processing]
Hint: atk [Processing]
Hint: pango [Processing]
Hint: gdk2pixbuf [Processing]
Hint: gdk2 [Processing]
Hint: cairo [Processing]
mygui.nim(23, 5) Error: type mismatch: got (PVBox, proc (): PEntry{.cdecl, gcsafe.})
but expected one of: 
proc add(x: var string; y: string)
proc add(x: var string; y: char)
proc add(factory: PIconFactory; stock_id: cstring; icon_set: PIconSet)
proc add(result: var string; x: int64)
proc add(x: var string; y: cstring)
proc add(items: PStockItem; n_items: guint)
proc add[T](x: var seq[T]; y: T)
proc add(container: PContainer; widget: PWidget)
proc add(result: var string; x: float)
proc add[T](x: var seq[T]; y: openArray[T])
proc add(list: PTargetList; target: gdk2.TAtom; flags: guint; info: guint)

我正在开发 Debian Stable Linux 并且已经从 Debian 存储库安装了 Nim 0.16.0。

问题出在哪里,如何解决?感谢您的帮助。

我不懂 nim 语言,但查看网络上的其他示例会让您的参考看起来很奇怪。这可能有效,但并不常见,请参阅:

错误消息说明了一切:

mygui.nim(23, 5) Error: type mismatch: got (PVBox, proc (): PEntry{.cdecl, gcsafe.})
but expected one of: 
proc add(x: var string; y: string)

该消息说您在第 23 行第 5 列传递了错误类型的参数。您使用一些参数调用 vbox.add,它需要另一种类型的参数。我不知道 nim,但这里使用的调用约定有歧义:

entry2 = entryNew

您将其解释为 "create an empty entry",但也可以将其视为 "put a reference to the procedure entryNew in the variable entry2",这似乎是问题所在,因此 got (PVBox, proc (): PEntry.

在我链接的其他示例中,人们使用括号,如下所示:

label2 = labelNew("second:")
entry2 = entryNew()

这对我来说似乎更合乎逻辑,因为它避免了歧义。也许这也行得通:

label2 = labelNew "second:"
entry2 = entryNew ""

但这对我来说很容易出错。