Hello world. Since a varargs[T] most be the last argument of its function, is there any way to pass a block to a template that uses varargs ? For exemple is it possible to write a version of withLock that acquires several locks at once ?
import locks
var
lk1, lk2, lk3 : Lock
v1 {.guard: lk1.} = 1 # The vars come from different contexts
v2 {.guard: lk2.} = 2 # Each one with its own lock
v3 {.guard: lk3.} = 3
proc blabla() {.thread.} =
withLock lk1, lk2, lk3:
echo v1, v2, v3
Beyond that question, I'm asking myself if it's necessary to always put a varargs at the end. I think that while there is only one varargs allowed per proc, we can avoid undecidable situations.
I imagine a Nim where:
It would be like this:
proc blabla(arg1: int, arg2: varargs['char'], arg3: int) =
echo arg1, arg2, arg3
blabla 3, 'd', 'e', 's', 'a', 7
I know that its not a fundamental question, and that things work already fine. We can use openArray as @juancarlospaco pointed out. I just don't know if there is a reason why varargs are so restrictive.This already works.
proc blabla(arg1: int, arg2: varargs[char], arg3: int) =
echo arg1, @arg2, arg3
blabla 3, 'd', 'e', 's', 'a', 7
Multiple varargs are also supposed to be disallowed, see issue.
It really works ! even without the @. The manual says about varargs: > This transformation is only done if the varargs parameter is the last parameter in the procedure header.
It should be updated...