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

**URL:** <https://discourse.julialang.org/t/error-cannot-convert-an-object-of-type-array-float64-3-to-an-object-of-type-float64/87928>\
**Category:** New to Julia\
**Tags:** question, convert\
**Created:** [September 28, 2022, 12:59pm UTC](https://discourse.julialang.org/t/error-cannot-convert-an-object-of-type-array-float64-3-to-an-object-of-type-float64/87928 "2022-09-28T12:59:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcket747](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcket747/32/43043_2.png) [@mcket747](https://discourse.julialang.org/u/mcket747)\
**Post date:** [September 28, 2022, 12:59pm UTC](https://discourse.julialang.org/t/error-cannot-convert-an-object-of-type-array-float64-3-to-an-object-of-type-float64/87928/1 "2022-09-28T12:59:57Z")

</div>

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.

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

```

---

<div class="post-metadata">

**Author:** ![Derek\_Vetsch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/derek_vetsch/32/8552_2.png) [@Derek\_Vetsch](https://discourse.julialang.org/u/Derek_Vetsch)\
**Post date:** [September 28, 2022, 1:59pm UTC](https://discourse.julialang.org/t/error-cannot-convert-an-object-of-type-array-float64-3-to-an-object-of-type-float64/87928/2 "2022-09-28T13:59:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mcket747](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcket747/32/43043_2.png) [@mcket747](https://discourse.julialang.org/u/mcket747)\
**Post date:** [September 29, 2022, 2:41pm UTC](https://discourse.julialang.org/t/error-cannot-convert-an-object-of-type-array-float64-3-to-an-object-of-type-float64/87928/3 "2022-09-29T14:41:35Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Derek\_Vetsch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/derek_vetsch/32/8552_2.png) [@Derek\_Vetsch](https://discourse.julialang.org/u/Derek_Vetsch)\
**Post date:** [September 29, 2022, 2:59pm UTC](https://discourse.julialang.org/t/error-cannot-convert-an-object-of-type-array-float64-3-to-an-object-of-type-float64/87928/4 "2022-09-29T14:59:18Z")

</div>

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](https://docs.julialang.org/en/v1/manual/performance-tips/#Type-declarations), 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.
