Consider the following code:
julia> function foo(T)
if T == Float64
v::Vector{Float64} = zeros(5)
else
v = T[1, 2, 3, 4, 5]
end
v
end
julia> function bar(T)
if T == Float64
v = zeros(5)
else
v = T[1, 2, 3, 4, 5]
end
v
end
julia> foo(Float32)
5-element Array{Float64,1}:
1.0
2.0
3.0
4.0
5.0
julia> bar(Float32)
5-element Array{Float32,1}:
1.0
2.0
3.0
4.0
5.0
I would have expected both foo
and bar
to behave basically in the same way, since the only difference are type annotations in a branch (which I needed for a more complex but similar example). However, the return types for T == Float32
are different.
- Is this expected behavior?
- If so, what is the best way to modify
foo/bar
such that the behavior forT == Float32
is the same as forbar
while I can help the compiler in inferring the type ofv
forT == Float64
? In my use case, the compiler isn’t able to figure out that type by itself. Should I just use dispatch onT
, e.g.
julia> function test(T::Type{Float64})
v::Vector{Float64} = zeros(5)
v
end
test (generic function with 2 methods)
julia> function test(T::DataType)
v = T[1, 2, 3, 4, 5]
v
end
test (generic function with 2 methods)
julia> test(Float64)
5-element Array{Float64,1}:
0.0
0.0
0.0
0.0
0.0
julia> test(Float32)
5-element Array{Float32,1}:
1.0
2.0
3.0
4.0
5.0