Recently I've been thinking about the fact that collect newSeq is too long of an expression, personally for me. So I tried to shorten it a bit, and also to learn how macros work.
This manner (c newSeq) is okay, it works:
import sugar, macros
macro c(init, body: untyped): untyped =
result = quote do:
collect `init`:
`body`
let x = c newSeq:
for i in 0..9:
i
echo x
But this one (simply c) doesn't:
import sugar, macros
macro c(body: untyped): untyped =
result = quote do:
collect newSeq:
`body`
let x = c:
for i in 0..9:
i
echo x
What am I doing wrong?
For me fails with
/tmp/test2.nim(8, 9) template/generic instantiation of `c` from here
/tmp/test2.nim(4, 13) template/generic instantiation of `collect` from here
/tmp/test2.nim(10, 7) Error: type mismatch: got <>
but expected one of:
proc (s: var seq[int], len: Natural){.noSideEffect.}
I get precisely the same error as greenfork.
It's Nim 1.4.0 on Linux, if it is important.
On 1.4.2 it works if you do this instead
import sugar, macros
macro c(body: untyped): untyped =
let newSeq = ident"newSeq"
result = quote do:
collect `newSeq`:
`body`
let x = c:
for i in 0..9:
i
echo x
it seems to be a problem with prematurely binding newSeq to a symbol