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?