But you can define a generic method for f like this:

f(a::Vector) = f(a, a[1])
f(a::Vector, a1::Int) = # handle the Int case
f(a::Vector, a1::Float64 = # handle the Float64 case


The you would call the function as `f(a)` the way you want to. Note that
the dispatch that chooses which method to call will be done at runtime,
which will be a bit slow (no worse than other dynamic languages, though).

On Fri, Aug 12, 2016 at 5:52 PM, Kristoffer Carlsson <[email protected]>
wrote:

> No, since the only thing the compiler sees is Vector{Any}.
>
>
> On Friday, August 12, 2016 at 11:36:54 PM UTC+2, Chris Stook wrote:
>>
>> Is there a way to dispatch on the type of the first element of an array
>> without passing the first element as a separate argument.
>>
>> for example:
>>
>> a = Array{Any,1}
>>
>> function f(a::Array{Any,1}, a1::Int)
>>   # do int stuff here
>> end
>> function f(a::Array{Any,1}, a1::Float64)
>>   # do float stuff here
>> end
>>
>> # call like this
>> f(a, a[1]) # better way?
>>
>> # would prefer to call like this
>> f(a)
>>
>> Thanks,
>> Chris
>>
>

Reply via email to