Hi,
I try to get the type of a parameter of a function similar to this fake - example :
macro typeofSecondParam(exp: expr): typedesc =
# echo lispRepr(exp)
return int
template forloop* (el, it: expr, loop: stmt): stmt {.immediate.} =
block:
var iter = it
var `el` : typeofSecondParam(next(iter, e))
while next(iter, el):
loop
template advance* (iter, times: expr): stmt =
block:
var el : typeofSecondParam(next(iter, e))
for i in 1..times:
if not next(iter, el): break
type
TCntUp = object
first, last, step, x: int
proc cntup(a, b: int, step = 1): TCntUp {.inline.} =
result.first = a
result.last = b
result.step = step
result.x = a
proc next(it: var TCntUp, el: var int): bool {.inline.} =
result = it.x <= it.last
if result :
el = it.x
inc(it.x, it.step)
cnt = 0
forLoop(i, cntup(1, cLoopMax)) :
cnt = cnt + i
BTW I don't want to reinvent the wheel, but I need an iterator which can be advanced outside the loop. And I feel that a hasNext, next pair is a) ugly because it needs 2 procs and b) is slower than the example above - which is as least as fast as the standard iteratorFor now, I think you need to pass the desired type explicitely.
Thanks for the interesting information. I don't understand the first one - what does it mean: templates will default to be hygienic ?
BTW I found a simple solution for my problem - the obvious one:
template forLoop* (x, it: expr, loop: stmt) : stmt {.immediate.} =
block:
var iter = it
var proceed {.noinit.}: bool
var `x` = next(iter, proceed)
while proceed :
loop
`x` = next(iter, proceed)
proc advance* [T](it: T): bool {.inline.} =
discard it.next(result)
proc next(it: var TCounterUp, proceed: var bool): int {.inline.} =
result = it.x
proceed = result <= it.last
if proceed :
inc(it.x, it.step)