Hello awesome people,
I'm a newbie to Nim, and trying to rewrite my game engine to it to learn. I've encountered a strange problem with template and untyped argument. I'm not sure what exactly is going on. It feels like compiler bug really, as the two examples bellow are only different on wheteher I pass batch: var BatchArg to the function. Once I do, the template's untyped argument is being checked and results in undeclared identifier.
However it must be the BatchArg thing, because if I pass just batch: var Batch it works. But I don't know how it relates to the template inside that is completely independent on that argument.
Any hints appreciated.
type Quad* = object
a*: int
type Batch* = object
a*: int
type BatchArg = Batch or ref Batch
# Works
proc testB*() =
var quad = Quad()
template attrib(member: untyped) =
echo quad.member
attrib(a)
testB()
# Doesn't work
proc testA*(batch: var BatchArg) =
var quad = Quad()
template attrib(member: untyped) =
echo quad.member
attrib(a) # << Error: undeclared identifier: 'a'
var batch = Batch()
testA(batch)
It works when you move template attrib out of your generic proc testA. I'm still not sure it's a compiler bug :P
The generic prepass that is supposed to prevent typos requires every identifier to be known. There is a special rule that prevents this for arguments passed to untyped templates but your local template attrib does not exist yet in the generic body so the special rule does not apply. Makes sense, right?
I didn't realize you can do it that way :P
I thought it can only be used to dynamic typed lang :D
Thanks!