I am defining a += template within the scope of another template.
When I call the outer template twice, I get
Error: ambiguous call; both in.+=(pgensym0: ptr T, offsetgensym0: int) [declared in /usercode/in.nim(2, 12)] and in.+=(pgensym1: ptr T, offsetgensym1: int) [declared in /usercode/in.nim(2, 12)] match for: (ptr int, int literal(1))
template ptrMath*(body: untyped) =
template `+=`[T](p: ptr T, offset: int) =
p = p + offset
body
when isMainModule:
ptrMath:
discard
var
a: array[0 .. 3, int]
p = addr(a[0])
ptrMath:
p += 1
Is this a bug in Nim, or in the above code?
nim
template ptrMath*(body: untyped) =
block:
proc `+=`[T](p: ptr T, offset: int) =
p[] = p[] + offset
body
when isMainModule:
ptrMath:
discard
var
a: array[0 .. 3, int]
p = addr(a[0])
ptrMath:
p += 1
Hello, I noticed that you changed p = p + offset to p[] = p[] + offset. But actually that isn't the intention here.
I have updated the code snippet, but I am unable to use the nested proc in there.