import macros
# cfunc expects a variable numebr of arguments, with first being a string that specifies types of the following args, e.g.
# cfunc("isis", 5, "foo", 7, "bar")
proc cfunc(bla: varargs[string, `$`]): string =
for s in items(bla):
result.add s
# wrapper without the first argument
macro wrapper(args: varargs[typed]): untyped =
var params = @[newLit("")]
var typestring = ""
for s in args:
case typeKind(getType(s)):
of ntyString: typestring.add("s")
of ntyInt: typestring.add("i")
else: discard
params.add(s)
params[0].strVal = typestring
result = newCall("cfunc", params)
# "tests"
echo wrapper(5, 7, "bar")
let
x = 4
y = 8
z = "bar"
echo wrapper(x, y, z, y)
As a practice is good, but you can even use templates:
template wrapper(arg: varargs[untyped]): string =
var h, t = ""
for f in fields((arg)):
when f is string: (h &= "s";t &= f;)
elif f is int: (h &= "i"; t &= $f;)
(h & t)
# "tests"
echo wrapper(5, 7, "bar")
let
x = 4
y = 8
z = "bar"
echo wrapper(x, y, z, y)