`Vararg` argument as optional argument gives error when there is a keyword argument

MWE:

julia> foo(a, b::Array{Int, 1}...=[0,0];c="") = @show b
foo (generic function with 3 methods)

julia> goo(a,b::Array{Array{Int, 1}, 1}) = begin
       c = "c"
       foo(a, b...;c)
       end
goo (generic function with 3 methods)

julia> goo(1, [[1,2,3]])
ERROR: TypeError: in typeassert, expected Vector{Int64}, got a value of type Tuple{Vector{Int64}}

julia> foo2(a, b::Array{Int, 1}...;c="") = @show b
foo2 (generic function with 1 method)

julia> goo2(a,b::Array{Array{Int, 1}, 1}) = begin
       c = "c"
       foo2(a, b...;c)
       end
goo2 (generic function with 1 method)

julia> goo2(1, [[1,2,3]])
b = ([1, 2, 3],)
([1, 2, 3],)

julia> foo3(a, b::Array{Int, 1}...=[0,0]) = @show b
foo3 (generic function with 2 methods)

julia> goo3(a,b::Array{Array{Int, 1}, 1}) = foo3(a, b...)
goo3 (generic function with 1 method)

julia> goo3(1, [[1,2,3]])
b = ([1, 2, 3],)
([1, 2, 3],)

Based on the differences among foo, foo2, and foo3, I suspect it might have something to do with the keyword argument syntax but I’m not sure.

I don’t know if this is technically a bug since I haven’t seen the usage of Vararg arguments as optional arguments on the official documentation (I might have missed it so feel free to point out); on the other hand since it’s not forbidden and yet we have this kind of errors. So I think it’s worth pointing out. Maybe fix the issue or warn people by throwing an error when they try to construct such a function? Thank you!

I think it’s a bug:

# works:
julia> foo(a,b::Array{Int, 1}... ;c="") = @show b
foo (generic function with 1 method)

julia> foo(1,[1,2] ; c="")
b = ([1, 2],)
([1, 2],)

julia> foo(1)
b = ()
()

# works:
julia> goo(a,b::Array{Int, 1}...=[0,0] ) = @show b
goo (generic function with 2 methods)

julia> goo(1)
b = ([0, 0],)
([0, 0],)

julia> goo(1,[1,2])
b = ([1, 2],)
([1, 2],)

# doesn't work:
julia> hoo(a,b::Array{Int, 1}...=[0,0] ;c="") = @show b
hoo (generic function with 2 methods)

I think it could work, so my guess it’s a bug. Do you mind opening an issue? Issues · JuliaLang/julia · GitHub

1 Like

Sure no problem. :ok_hand: I just wanted to make sure if I missed something.

2 Likes

https://github.com/JuliaLang/julia/issues/40964

1 Like