(Idiomatic) functions on vectors containing elements of various types

I suppose you meant bar([x,y]), otherwise you are not passing a vector.

The point is, that in the absence of a rule, A and B promote to Any. Julia doesn’t promote to Union types by default:

julia> promote_type(String, Int)
Any

It is easy to define an appropriate rule though:

julia> import Base: promote_rule # import to overload

julia> struct A end; struct B end; struct C end;

julia> Foo = Union{A,B,C}
Union{A, B, C}

julia> promote_rule(::Type{S}, ::Type{T}) where {S<:Foo, T<:Foo} = Foo
promote_rule (generic function with 125 methods)

julia> [A(),B(),C()]
3-element Vector{Union{A, B, C}}:
 A()
 B()
 C()

Now the function bar dispatches correctly

julia> bar([A(),B()])
A()
B()

julia> bar([A(),B(),"Spoiled!"])
ERROR: MethodError: no method matching bar(::Vector{Any})
Closest candidates are:
  bar(::Vector{T}) where T<:Union{A, B, C} at REPL[6]:1
Stacktrace:
 [1] top-level scope
   @ REPL[9]:1
2 Likes