I frequently find myself writing functions which are fundamentally not that different for scalars or vectors. Take this for example:
julia> function silly_print(x)
for i = 1:length(x)
println(x[i])
end
end
julia> silly_print(2)
2
julia> silly_print([2,4])
2
4
Specifying that x is a Vector breaks this behaviour:
julia> function silly_print_vector(x::Vector)
for i = 1:length(x)
println(x[i])
end
end
silly_print_vector (generic function with 1 method)
julia> silly_print_vector(2)
ERROR: MethodError: no method matching silly_print_vector(::Int64)
Closest candidates are:
silly_print_vector(::Array{T,1} where T) at none:1
Stacktrace:
[1] top-level scope at none:0
Is there a workaround for this, without writing two identical functions but for different types?