Are Vararg only allowed as a final argument and if so why?

For example

julia> f(x::Vararg{Int,N}, y::Float64) where {N} = (x, y)

ERROR: ArgumentError: Vararg on non-final argument in method definition for f at REPL[9]:1

In principle the method signature is not ambiguous so it’s not clear why this doesn’t work.
Is this just a temporary limitation or more fundamental ?

Quoth the Jeff:

This is absolutely not supported yet. It would be very useful, but extremely difficult.

2 Likes

It does not work because Vararg is defined to work if and only if it is the last parameter.
To pass some varying number of values as your x, use a Tuple (1, 2) (1, 2, 3) etc
where f(x::Tuple, y::Float64).

It isn’t really a limitation since you can’t have more than one vararg in the method signature, no matter what. Having it in a location other than the end will only add unnecessary complexity.

Actually there are use cases, see for example https://github.com/JuliaLang/julia/pull/31654

the API would be more uniform in that case if the size argument came last, but it seems it is not possible since it would require a vararg as a first argument.

In any case, it should at least be easiest to allow to be either the first or last argument in a method.

The issue is closed, but the problem is not. Maybe accidental?

The issue was closed by fixing the other bug it listed and improving the error message.