Error: Cannot 'Convert' an object of type Array{Float64,3} to an object of type float64

Hello,

I am very new to Julia and am running into this error. Operations with similar code have not returned this error so Im not sure what I’m doing wrong. I run the following code and get the can’t convert error. Any advice is appreciated.

mutable struct Results
    r:: Float64
    w:: Float64
    b:: Float64
    pol_func:: Array{Float64,3}
    val_func:: Array{Float64,3}
    #asset_func:: Array{Float64,3}
    lab_func:: Array{Float64,3}
    age_j:: Int64
    mu:: Array{Float64,2}
    K:: Float64
    L:: Float64
    #K::Array{Float64,1}
    #L::Array{Float74,1}
end

function Initialize()
    prim = Primitives()
    pol_func = zeros(prim.na,prim.nz,prim.N)
    val_func = zeros(prim.na,prim.nz,prim.N)
    lab_func = zeros(prim.na,prim.nz,prim.N)
    r = 0.05
    w = 1.05
    b = 0.2
    age_j = 66
    mu = zeros(prim.na,prim.nz,prim.N)
    K = 3
    L = 0.5
    res = Results(pol_func,val_func, lab_func,age_j, mu, r,w,b, K, L)
    prim, res
end
prim, res = Initialize()
MethodError: Cannot `convert` an object of type Array{Float64, 3} to an object of type Float64
Closest candidates are:
  convert(::Type{T}, !Matched::Gray24) where T<:Real at C:\Users\mcket\.julia\packages\ColorTypes\1dGw6\src\conversions.jl:114
  convert(::Type{T}, !Matched::Gray) where T<:Real at C:\Users\mcket\.julia\packages\ColorTypes\1dGw6\src\conversions.jl:113
  convert(::Type{T}, !Matched::T) where T<:Number at C:\Users\mcket\AppData\Local\Programs\Julia-1.7.3\share\julia\base\number.jl:6
  ...
Results(r::Array{Float64, 3}, w::Array{Float64, 3}, b::Array{Float64, 3}, pol_func::Int64, val_func::Array{Float64, 3}, lab_func::Float64, age_j::Float64, mu::Float64, K::Int64, L::Float64) at pset3_code.jl:45
Initialize() at pset3_code.jl:72
top-level scope at pset3_code.jl:150
eval at boot.jl:373 [inlined]

It looks like you are supplying fields to Results in the wrong order. When you do res = Results(pol_func, val_func, lab_func, age_j, mu, r, w, b, K, L), the first argument, pol_func is of type Array{Float64, 3}. However, when you set up your Results struct, you defined the first field to have type Float64.

To solve, simply change the order of the arguments in the res = Results line.

Lastly, as purely a stylistic note… Are you sure you need the type parameters for the fields in your struct? When people start with Julia, a common frustration is putting types on everything, then running into MethodErrors because the Julia type tree is large and there can be many corner cases that aren’t initially intuitive.

1 Like

Ah thank you! This worked. And do you mean if I need to add things like Float64? I have actually run into an error like that because of having the wrong type. Is it more trouble than its worth?

Exactly - for you to actually instantiate a Results object, the values need to be coercible to Float64. While it’s true that in the Julia manual, it does say to avoid ambiguous type parameters, especially when defining your own types, I found that when I started learning Julia, I would annotate everything and then get frustrated about random errors.

Essentially, the type annotations in your struct make your code easier for the compiler to optimize, but at the cost of your code being more rigid. I tend to think that it’s better to write working code first, then worry about performance improvements.

However, if you are a fairly experienced programmer (which I was not when I learned Julia), this may not be an issue to you. Hopefully that helps.

1 Like