# Cthulhu.jl showing unstable return type

**URL:** <https://discourse.julialang.org/t/cthulhu-jl-showing-unstable-return-type/109385>\
**Category:** Performance\
**Created:** [January 29, 2024, 10:30am UTC](https://discourse.julialang.org/t/cthulhu-jl-showing-unstable-return-type/109385 "2024-01-29T10:30:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gokhan\_Kof](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gokhan_kof/32/43267_2.png) [@Gokhan\_Kof](https://discourse.julialang.org/u/Gokhan_Kof)\
**Post date:** [January 29, 2024, 10:30am UTC](https://discourse.julialang.org/t/cthulhu-jl-showing-unstable-return-type/109385/1 "2024-01-29T10:30:08Z")

</div>

Hi,

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`.

```julia
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)]
end
@descend my_func(my_dict, my_set)

```

```julia
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]
   ↩

```

---

<div class="post-metadata">

**Author:** ![magister-ludi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/magister-ludi/32/4003_2.png) [@magister-ludi](https://discourse.julialang.org/u/magister-ludi)\
**Post date:** [January 29, 2024, 10:50am UTC](https://discourse.julialang.org/t/cthulhu-jl-showing-unstable-return-type/109385/2 "2024-01-29T10:50:38Z")

</div>

`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 `Array`s is 1.

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

---

<div class="post-metadata">

**Author:** ![Gokhan\_Kof](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gokhan_kof/32/43267_2.png) [@Gokhan\_Kof](https://discourse.julialang.org/u/Gokhan_Kof)\
**Post date:** [January 29, 2024, 10:55am UTC](https://discourse.julialang.org/t/cthulhu-jl-showing-unstable-return-type/109385/3 "2024-01-29T10:55:30Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [January 29, 2024, 11:21am UTC](https://discourse.julialang.org/t/cthulhu-jl-showing-unstable-return-type/109385/4 "2024-01-29T11:21:51Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![magister-ludi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/magister-ludi/32/4003_2.png) [@magister-ludi](https://discourse.julialang.org/u/magister-ludi)\
**Post date:** [January 29, 2024, 12:13pm UTC](https://discourse.julialang.org/t/cthulhu-jl-showing-unstable-return-type/109385/5 "2024-01-29T12:13:33Z")

</div>

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

```julia
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.

---

<div class="post-metadata">

**Author:** ![Gokhan\_Kof](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gokhan_kof/32/43267_2.png) [@Gokhan\_Kof](https://discourse.julialang.org/u/Gokhan_Kof)\
**Post date:** [January 29, 2024, 2:59pm UTC](https://discourse.julialang.org/t/cthulhu-jl-showing-unstable-return-type/109385/6 "2024-01-29T14:59:00Z")

</div>

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