Hi! I'm beginning to write some templates and I'm just not sure how to properly
use them in the right way. Macros might be an option too, but they look much
more complicated and I would have no idea how to solve it with macros.
Anyway - I want to have some "callback" function that can be called from
somewhere else, maybe even from a C-library. This callback may take multiple
mixed parameters, int and string. I could create and hard-code some wrappers to
make it fit to the callback if possible, and throw an error if it doesn't fit,
but I thought it might be easier if I use some kind of abstraction layer to
reduce copy&paste code.
So - I tried to have some kind of template "runWithBody" that might already
check specific lengths and may throw exceptions, if something doesn't fit - for
example the length. But the parameters "values" aren't injected to the body
somehow. Below is a simplified example that already shows my issue.
I'm probably doing something stupid here. I'm just not sure what would be smart
in such a case :)
var callback: proc(args: varargs[int]): string # this line shouldn't change
template runWithBody(minLength: int, body: untyped) =
proc localFunc(values: openArray[int]): string =
if values.len >= minLength:
body
else:
raise newException(ServerException, "Called function contains less
than " & $minLength & " arguments")
callback = proc(args: varargs[int]): string =
localFunc(args)
runWithBody(2):
result = $(values[0] + values[1]) # here is the problem: Error:
undeclared identifier: 'values'
print callback(1, 2, 3) # this line also shouldn't change, if possible.
Run