i got a strange issue with Concepts and macros:
if have an iterator like this:
type
Loopable*[T] = concept x {.explain.}
x.len() is int
x[int] is T
x.items is T
Elem = object
baa*: string
iterator loop*[T](a: Loopable[T]): tuple[loop: Loop[T], val: T] =
# ...
in my template engine i want to write this code:
{% for (loop, elem) in elems.loop() %}
{{ elem.baa }}
{% endfor %}
the macro for "{{ }}" basically is this:
proc astVariable(token: NwtNode): NimNode =
var varb: NimNode
varb = parseStmt(token.variableBody)
return nnkStmtList.newTree(
nnkInfix.newTree(
newIdentNode("&="),
newIdentNode("result"),
newCall(
"$",
varb
)
)
)
the strange thing now is, when the loop is a concept:
loop*[T](a: Loopable[T]): tuple[loop: Loop[T], val: T] =
i get the error:
: undeclared field: 'baa'
when i change to a openArray it works...
loop*[T](a: openArray[T]): tuple[loop: Loop[T], val: T] =
any idea?
Seems unrelated to the macro expansion and instead is probably a bug with concepts. This is a minimal example that reproduces your error. Please file an issue on Github.
type
Loopable*[T] = concept x
x.len() is int
x[int] is T
x.items is T
iterator loop*[T](a: Loopable[T]): T = # TODO cannot access fields in iterator
for each in a:
yield each
type
Elem* = object
baa*: string
proc foo(elems: seq[Elem]): string =
for elem in elems.loop():
result &= "\n "
result &= $(elem.baa)
result &= "\n"
let elems = @[Elem(baa: "one"), Elem(baa: "two")]
echo foo(elems)