Ok, that makes sense. In that case what would be the best way to write a
funcion that accepts several Real arrays (of not necessarily the same
type)? Currently I'm using
function f{T1,T2,T3 <: Real}(x::Array{T1}, y::Array{T2}, z::Array{T3})
...
end
But that gets messy very quickly if there are many arguments.
On Friday, June 26, 2015 at 6:00:25 PM UTC+2, Tim Wheeler wrote:
>
> Hello Linus,
>
> This is based on how array types are defined. In general, Vector{subtype}
> is not a subtype of Vector{supertype}.
> Try this:
>
> f{R<:Real}(x::Array{R}) = x
>
> -Tim
>
> On Friday, June 26, 2015 at 8:38:12 AM UTC-7, Linus Härenstam-Nielsen
> wrote:
>>
>> I ran into a problem with types today that I don't know how to interpret.
>>
>> If I define a function according to
>> f(x::Real) = x
>> it returns x as expected as long as the type of x is a subtype of Real.
>> However if I define
>> f(x::Array{Real}) = x
>> it throws MethodError no matter what I pass as argument. For example when
>> I try to pass it an Int array:
>> f([1 2 3; 3 4 5])
>> ERROR: MethodError: `f` has no method matching f(::Array{Int64,2})
>>
>> I would expect it to work like
>> f{T<:Real}(x::Array{T}) = x
>> and accept any array of reals. Am I missing something?
>>
>>
>>
>>