document macros.unpackVarargs (#18106)

* deprecate macros.unpackVarargs

* un-deprecate unpackVarargs and add docs+runnableExamples

* update examples + tests with varargs[typed]
This commit is contained in:
Timothee Cour 2021-05-31 01:51:20 -07:00 • committed by GitHub
commit 18b4774311
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 0 deletions

View file

@ -1682,6 +1682,21 @@ macro getCustomPragmaVal*(n: typed, cp: typed): untyped =
error(n.repr & " doesn't have a pragma named " & cp.repr, n)
macro unpackVarargs*(callee: untyped; args: varargs[untyped]): untyped =
## Calls `callee` with `args` unpacked as individual arguments.
## This is useful in 2 cases:
## * when forwarding `varargs[T]` for some typed `T`
## * when forwarding `varargs[untyped]` when `args` can potentially be empty,
## due to a compiler limitation
runnableExamples:
template call1(fun: typed; args: varargs[untyped]): untyped =
unpackVarargs(fun, args)
# when varargsLen(args) > 0: fun(args) else: fun() # this would also work
template call2(fun: typed; args: varargs[typed]): untyped =
unpackVarargs(fun, args)
proc fn1(a = 0, b = 1) = discard (a, b)
call1(fn1, 10, 11)
call1(fn1) # `args` is empty in this case
if false: call2(echo, 10, 11) # would print 1011
result = newCall(callee)
for i in 0 ..< args.len:
result.add args[i]