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:
parent
98ea61f09b
commit
18b4774311
3 changed files with 66 additions and 0 deletions
|
|
@ -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]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue