Getting this type signature correct

When I use your second definition, test(1, 2) works for me, but test(1, :) does not:

julia> function test(i::P, I::P...) where P <: Union{Int, Colon, OrdinalRange}
           println((i, I...))
       end
test (generic function with 1 method)

julia> test(1, 2)
(1, 2)

julia> test(1, :)
ERROR: MethodError: no method matching test(::Int64, ::Colon)
Closest candidates are:
  test(::P, ::P...) where P<:Union{Colon, Int64, OrdinalRange} at REPL[2]:1

The reason is because the second definition declares that all the arguments are of the same type P. So your second definition is not equivalent to the first definition, because in your first definition the arguments can be of different types.

1 Like