> See the manual I linked. Float64 <: Number does not imply
> Vector{Float64} <: Vector{Number}

Sorry, I didn't notice it. Thanks!

But what is then a proper way to define a function whose argument is
either Vector{Float64} or Vector{Int64}?


On 1/9/2016 8:17 PM, Yichao Yu wrote:
> On Sat, Jan 9, 2016 at 1:48 PM, Alexandr <[email protected]> wrote:
>> I am using Version 0.4.0 (2015-10-08 06:20 UTC)
>>
>> I restarted Julia repl/intrpreter.
>>
>> Below is a complete code where I am trying to solve my task in a two ways:
>>
>> 1) by defining a Union of the types
>>
>> 2) by specifying the type of elements in the input vector as "Number".
>> (Is it correct to do it?)
>> Because according to the types hierarchy here:
>> https://en.wikibooks.org/wiki/Introducing_Julia/Types
>> both Int and Float are subtypes of super type Number and Real.
>>
>> #------------------------------------#
>> IntOrFloat = Union{Int, AbstractFloat}
>>
>> function ff1(v::Array{IntOrFloat,1})
>>      mean(v)
>> end
>> println( ff1([1.0, 2.0]) )
>>
>> function ff2(v::Vector{IntOrFloat})
>>      mean(v)
>> end
>> println( ff2([1.0, 2.0]) )
>>
>> function ff3(v::Vector{Number})
>>     filter(x-> x>=1.5, v)
>> end
>> ff3([1.0, 2.0, 3.0, 5.0])
>>
>> #------------------------------------#
>> Output errors:
>>
>> ERROR: LoadError: MethodError: `ff1` has no method matching
>> ff1(::Array{Float64,1})
>>
>> ERROR: LoadError: MethodError: `ff2` has no method matching
>> ff2(::Array{Float64,1})
>>
>> ERROR: LoadError: MethodError: `ff3` has no method matching
>> ff3(::Array{Float64,1})
>>
>> #------------------------------------#
> 
> The signature of the first and second functions are exactly the same
> and all three gives error as expected.
> 
> See the manual I linked. Float64 <: Number does not imply
> Vector{Float64} <: Vector{Number}
> 
>>
>>
>> On 1/9/2016 7:32 PM, Yichao Yu wrote:
>>>>
>>>> Which version are you using? `f1` shouldn't match either and it's a
>>>> bug if it does.
>>>
>>> Also check if you've already defined another `f1` that does accept the
>>> arrays of different types.
>>>
>>> And for why neither of them should match. See
>>> http://julia.readthedocs.org/en/latest/manual/types/#parametric-composite-types
>>>
>>>>
>>>>>
>>>>> --
>>>>> Alexandr
>>
>> --
>> Alexandr

-- 
Alexandr

Reply via email to