Cthulhu.jl showing unstable return type


I am trying to eliminate type unstable parts of my code and Cthulhu.jl is showing the return type of a dictionary access as red:

x = my_dict[first(my_set)]

x is detected as as Array{Float64} but shown in red. I suspect that the return type of first can be the type of the elements in the set or it can be Nothing.

using Cthulhu
my_dict = Dict{Float64, Array{Float64}}(3. => [2., 5.], 2. => [3., 2.], 7. => [4., 4.])
my_set = Set([1., 7., 3.])
function my_func(D::Dict{Float64, Array{Float64}}, S::Set{Float64})
  return D[first(S)]
@descend my_func(my_dict, my_set)
my_func(D::Dict{Float64, Array{Float64}}, S::Set{Float64}) @ Main REPL[4]:1
1 function my_func(D::Dict{Float64, Array{Float64}}::Dict{Float64, Array{Float64}}, S::Set{Float64}::Set{Float64})::Array{Float64}
2   return D::Dict{Float64, Array{Float64}}[first(S::Set{Float64})::Float64]::Array{Float64}
3 end
Select a call to descend into or ↩ to ascend. [q]uit. [b]ookmark.
Toggles: [w]arn, [h]ide type-stable statements, [t]ype annotations, [s]yntax highlight for Source/LLVM/Native, [j]ump to source always.
Show: [S]ource code, [A]ST, [T]yped code, [L]LVM IR, [N]ative code
Actions: [E]dit source code, [R]evise and redisplay
 • first(S::Set{Float64})
   D::Dict{Float64, Array{Float64}}[first(S::Set{Float64})::Float64]

Array{Float64} is an abstract type: an Array has both an element type (in your case, Float64) and a dimension. In your example, the dimension of the Arrays is 1.

You should use either Array{Float64, 1}, or the commonly used name Vector{Float64}.


Right. So If I want to be flexible about the dimension of Array{Float64}, I cannot do that with a type stable code unless I convert any multi-dimensional Array to a Vector? I think that’s doable in my case. Thank you!

If you want myfunc to be flexible so that it can return either an Array{Float64, 1} or an Array{Float64, 2} for example, then by definition it’s not type stable.

The usual solution would be to do as you say, reshape the array to 1 dimension (using vec or reshape is efficient, it doesn’t copy the data).

1 Like

No it doesn’t mean that. I used Vector because you used one-dimensional arrays in your example. What you do need to do in order to remove the warning is make your arrays concrete types.

If your arrays all have the same dimensionality, you can declare the function using

function my_func(D::Dict{Float64, Array{Float64, N}, s::Set{Float64}) where {N}

If, however, the Dict contains arrays with different dimensionalities, then the warning will remain unless you can find a different way to represent your data.

1 Like

Yeah, I used the Vector as an example, too. I meant that my Arrays does not have the same dimensionality. So, I can convert them all into Vectors.

1 Like