I am making a macro which will take this code:
gnat newDataWindow(data: ref MyData):
window_new(gtk2.WINDOW_TOPLEVEL):
...
And turn it into something like this:
proc newDataWindow(data: ref MyData): PWindow =
var widget = window_new(gtk2.WINDOW_TOPLEVEL)
...
return widget
My problem is, how do I get the type that window_new will return, so that I can get the correct return type for newDataWindow (PWindow)?
I can use a template instead of proc, and it seems to work OK, but that's not what I really want.
I found an answer. I still don't know if it's possible to get the return type of window_new(..) inside the macro (that is, to get the PWindow type as a NimNode). I can imagine it might not be technically possible, but I would still like an answer to that to be sure.
But I can generate code that satisfies my requirement:
proc newDataWindow(data: ref MyData): type(window_new(gtk2.WINDOW_TOPLEVEL)) =
...
or
type TnewDataWindow = type(window_new(gtk2.WINDOW_TOPLEVEL))
proc newDataWindow(data: ref MyData): TnewDataWindow =
...
I think your solution is fine. You can also use the auto type, but it might not work in some cases.
The macros module has something for getting the type nowadays too: http://nim-lang.org/docs/macros.html#getType,NimNode IIRC.