import macros
import typetraits
import strformat
macro fun*(n: varargs[untyped]): typed =
result = newNimNode(nnkStmtList, n)
for x in n:
result.add(newCall("echo", x))
result.add(newCall("echo", newStrLitNode($(n.len))))
# void-return proc
proc bar() = echo "ok"
fun(1+1, "abc", 42) # n.len=3
fun(1+1, "abc") # n.len=2
fun(1+1) # n.len=1
fun() # n.len=0
# gives Error: type mismatch: got <void>
fun(bar()) # n.len=1 ?? why not 0?
in D it works correctly and doesn't break generic code:
void fun(T...)(T n){
static foreach(x:n)
writeln(x);
writeln(T.length);
}
void bar(){writeln("ok");}
fun(1+1, "abc", 42);
fun(1+1, "abc");
fun(1+1);
fun();
fun(bar());
* is that nim choice by design or could that be fixed so that n.len = 0 in
above code?
* how would we work around it in my macro fun to detect whether the type of x
is void and proceed accordingly?