The short version is that a function typed as
function x1(Array{SuperType,1})
will throw an error if an argument which is an array of only subtypes is passed but not if the array is composed of elements with a mix of subtypes, i.e. if we have subtypes A1 and A2, x1([A1, A2]) works, but x1([A1,A2]) does not work.
I’m not quite sure I understand why.
Here’s a concrete example to demonstrate.
abstract type Foo end
struct Bar <: Foo
a :: Int
end
struct Baz <: Foo
a :: Float64
end
function x1(z::Array{Foo,1})
println(z)
end
w1 = Bar(1)
w2 = Baz(1.0)
w = [ w1, w2 ]
x1(w)
w = Foo[w1, w1]
x1(w)
w = [w1, w1]
x1(w)
Foo[Bar(1), Baz(1.0)]
Foo[Bar(1), Bar(1)]
briand@quarternote:~/src/julia/fdtd$ julia test5.jl
Foo[Bar(1), Baz(1.0)]
Foo[Bar(1), Bar(1)]
ERROR: LoadError: MethodError: no method matching x1(::Array{Bar,1})
Closest candidates are:
x1(::Array{Foo,1}) at /home//src/julia/fdtd/test5.jl:12
Stacktrace:
[1] top-level scope at /home//src/julia/fdtd/test5.jl:29
[2] include(::Function, ::Module, ::String) at ./Base.jl:380
[3] include(::Module, ::String) at ./Base.jl:368
[4] exec_options(::Base.JLOptions) at ./client.jl:296
[5] _start() at ./client.jl:506
in expression starting at /home//src/julia/fdtd/test5.jl:29