Hello people, new user of nim here and I am already excited! I have the following scenario, for test purposes, and I think I'm missing something here, or, in other words, how to make the compiler get what I mean?
proc test(names:varargs[seq[string]]) = discard
proc test(names:varargs[string]) = discard
test "hello", "world" # works
test @["hello"], @["world"] # works
test @["hello", "world"] # compiler error, I get an "ambiguous call"
In my eyes this is not an "ambiguous call", since this is clearly a seq and not a varags. And this is obvious for me, since the two types are not interchangeable. So what I am missing here?
In this example
proc test(names:varargs[string]) =
for n in names:
echo n
echo "----"
test "hello", "world"
test @["hello", "world"]
both calls work. The first call passes string arguments, they are implicitly converted to an openArray for the varargs proc as usual. Looking at the second call, the compiler thinks that you already did that conversion because it gets a seq, which is an openArray. So for the last call in your example, the compiler cannot decide whether you are trying to call the first proc with one argument or the second proc with two arguments. Thanks!
openarray matches with seq and arrays varargs matches with seq, arrays and variadic arguments like foo(1, 2, 3)
You can only hint the compiler if they are in different files like fileA and fileB, you can use fileA.test and fileB.test
A varargs parameter is an openarray parameter that additionally allows to pass a variable number of arguments to a procedure. The compiler converts the list of arguments to an array implicitly
Openarrays are always indexed with an int starting at position 0. The len, low and high operations are available for open arrays too.
I think this documentation should clearly state that strings, seqs and arrays are openarrays (actually the only ones).