gtkgtk2nim-lang

Nim with gtk2 not compiling


I am trying following code which is modified from http://rosettacode.org/wiki/Simple_windowed_application#Nim (that code compiles without any problem):

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

I am using following command:

nim c -r mygui.nim 

However, it is giving following error output:

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)

I am working on Debian Stable Linux and have installed Nim 0.16.0 from Debian repository.

Where is the problem and how can it be solved? Thanks for your help.


Solution

  • I don't know the nim language, but looking at other examples on the web makes your reference looks weird. That may be valid, but not common, see:

    The error message says it all:

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

    The message says you're passing the wrong type of argument on line 23, column 5. You call vbox.add with some arguments and it expects arguments of another type. I don't know nim, but the calling convention used here is ambiguous:

    entry2 = entryNew
    

    You interpreted this as "create an empty entry", but it can also be seen as "put a reference to the procedure entryNew in the variable entry2", which seems the problem here, hence the got (PVBox, proc (): PEntry.

    On the other examples I linked to, people use parenthesis, like this:

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

    That seems more logical to me, as it avoids the ambiguity. Maybe this would work too:

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

    But that looks error-prone to me.