When i use following code i have no problems with types
using LinearAlgebra, BlockDiagonals
function f1(x::Vector{T}, ::Int, ::Val{:A}) where T
Diagonal(x)
end
function f1(x::Vector{T}, i::Int, ::Val{:B}) where T
Diagonal(x .* i)
end
function f1(x::Vector{T}, ::Int, ::Val{:C}) where T
Diagonal(x)
end
function f1(x::Vector{T}, ::Int, ::Val{:D}) where T
Diagonal(x)
end
function f2(x1, x2, a, b)
x = Vector{Matrix}(undef, 2)
x[1] = f1(x1, 2, Val{a}())
x[2] = f1(x2, 3, Val{b}())
BlockDiagonal(x)
end
@code_warntype f2([1,2,3], [2,3,4], :A,
@code_warntype:
#self#::Core.Compiler.Const(f2, false)
x1::Array{Int64,1}
x2::Array{Int64,1}
a::Symbol
b::Symbol
x::Array{Array{T,2} where T,1}
Body::Union{}
1 β %1 = Core.apply_type(Main.Vector, Main.Matrix)::Core.Compiler.Const(Array{Array{T,2} where T,1}, false)
β (x = (%1)(Main.undef, 2))
β %3 = Core.apply_type(Main.Val, a)::Type{Val{_A}} where _A
β %4 = (%3)()::Val{_A} where _A
β %5 = Main.f1(x1, 2, %4)::Diagonal{Int64,Array{Int64,1}}
β Base.setindex!(x, %5, 1)
β %7 = Core.apply_type(Main.Val, b)::Type{Val{_A}} where _A
β %8 = (%7)()::Val{_A} where _A
β %9 = Main.f1(x2, 3, %8)::Diagonal{Int64,Array{Int64,1}}
β Base.setindex!(x, %9, 2)
β Main.BlockDiagonal(x)
βββ Core.Compiler.Const(:(return %11), false)
But when I add one more functionβ¦
using LinearAlgebra, BlockDiagonals
function f1(x::Vector{T}, ::Int, ::Val{:A}) where T
Diagonal(x)
end
function f1(x::Vector{T}, i::Int, ::Val{:B}) where T
Diagonal(x .* i)
end
function f1(x::Vector{T}, ::Int, ::Val{:C}) where T
Diagonal(x)
end
function f1(x::Vector{T}, ::Int, ::Val{:D}) where T
Diagonal(x)
end
function f1(x::Vector{T}, ::Int, ::Val{:E}) where T
Diagonal(x)
end
function f2(x1, x2, a, b)
x = Vector{Matrix}(undef, 2)
x[1] = f1(x1, 2, Val{a}())
x[2] = f1(x2, 3, Val{b}())
BlockDiagonal(x)
end
@code_warntype f2([1,2,3], [2,3,4], :A, :B)
I nave this: Main.f1(x2, 3, %8)::Any
Variables
#self#::Core.Compiler.Const(f2, false)
x1::Array{Int64,1}
x2::Array{Int64,1}
a::Symbol
b::Symbol
x::Array{Array{T,2} where T,1}
Body::Union{}
1 β %1 = Core.apply_type(Main.Vector, Main.Matrix)::Core.Compiler.Const(Array{Array{T,2} where T,1}, false)
β (x = (%1)(Main.undef, 2))
β %3 = Core.apply_type(Main.Val, a)::Type{Val{_A}} where _A
β %4 = (%3)()::Val{_A} where _A
β %5 = Main.f1(x1, 2, %4)::Any
β Base.setindex!(x, %5, 1)
β %7 = Core.apply_type(Main.Val, b)::Type{Val{_A}} where _A
β %8 = (%7)()::Val{_A} where _A
β %9 = Main.f1(x2, 3, %8)::Any
β Base.setindex!(x, %9, 2)
β Main.BlockDiagonal(x)
βββ Core.Compiler.Const(:(return %11), false)
How I can make type stable code with dispatch by Val() and more than 4 functions?