Based on
http://nim-lang.org/docs/manual.html#templates-identifier-construction
I had from https://github.com/ngtk3/nim-gobject/blob/master/src/gobject.nim
template gStructOffset*(typ, field): expr = (var dummy: typ; clong(cast[system.int](addr(dummy.field)) - cast[system.int](addr(dummy))))
template gPrivateOffset*(TypeName, field): expr =
`TypeName privateOffset` + gStructOffset(`TypeName PrivateObj`, field)
Compiles fine, but it is wrong: While gPrivateOffset() works OK, gStructOffset() does not. The correct way to fake the glib macros would be to add "Private" only in gPrivateOffset() and to add "Obj" in gStructOffset. I tried more than one our...
Working solution is to make these templates independent from each other, but it looks not really nice, because of a lot repeated code:
template gStructOffset*(typ, field): expr = (var dummy: `typ Obj`; clong(cast[system.int](addr(dummy.field)) - cast[system.int](addr(dummy))))
template gPrivateOffset*(typ, field): expr = (var dummy: `typ PrivateObj`; clong(`typ privateOffset` + cast[system.int](addr(dummy.field)) - cast[system.int](addr(dummy))))
Is there a nicer way to express this? Basically the task is nested templates with Identifier construction -- and I got identifiers with nested backticks, which does not compile.