On Sunday, May 25, 2014 3:39:33 AM UTC-4, [email protected] wrote:
> But,
> function foo(a::Array{FloatingPoint}, b::Array{FloatingPoint})
> a + b
> end
> is unfortunately not working. I don't know whether this is a bug or a
> choice.
>
This is not a bug, it just doesn't mean what you think. (This is such a
common question, it really should be in the FAQ.)
An Array{Float64} is not an instance of Array{FloatingPoint}.
a::Array{FloatingPoint} is an array of generic "boxes" which can contain
any floating-point type, potentially a different type for *each element*;
e.g. a[1] could be a Float64, a[2] could be a Float32, a[3] could be a
BigFloat, etcetera. An Array{Float64} is an array where *every element*
is a Float64, and hence this can be stored and computed with much more
efficiently than an Array{FloatingPoint}.
What you want is
function foo{T1<:FloatingPoint, T2<:FloatingPoint)(a::Array{T2},
b::Array{T2})
a + b
end
This says that the arrays can each be of any floating-point type, but the
array elements are all of the same type.
You could also make the code a bit more generic with:
function foo{T1<:FloatingPoint, T2<:FloatingPoint)(a::AbstractArray{T2},
b::AbstractArray{T2})
a + b
end
This allows the arguments to be any subtype of AbstractArray (which
includes Array but also things like ranges).