# B.nim
type T* = object
a: int
proc createT*(x: int) : T =
result.a = x
proc b(t: T) : int = t.a
template doAction*(action: stmt) {.immediate.} =
action
# A.nim
import B
var t = createT(3)
doAction:
t.a += 1 # this works
echo t.a # this works
echo t.b # this doesn't work (as expected)
This is supported so that template accessor*(x: T): int = x.a can work. But maybe we should support that differently to prevent your code example from compiling.
and what other scope-related pitfalls should I be aware of?
This question is impossible to answer. Feature vs bug is a difficult question when it comes to scoping rules in a macro system.
template doSomething*(v: expr, s: stmt) {.immediate.} =
let `v` {.inject.} = ...
s
finalize
Currently, the two options that I know of are just to keep it as is and hope no confusion arises, or move T to a new file and export it from B.
# B_private.nim
type T* = object
aval : int
proc a*(t: T) = t.aval
proc `a=`*(t: var T, k: int) = t.aval = k