How to allocate an array according to the type of a computation to follow?

I tried the following example

function test(a,b, obs)
    T = typeof(a * b)
    Z = Array{T}(obs)
    for i in 1:obs
        Z[i] = a * b / i
    end
    return Z
end
test(5.0,2.0, 3)
test(Dual(5.0, 1,0), Dual(2.0, 0,1), 3)

but it errors with ERROR: LoadError: MethodError: no method matching (Array{Float64, N} where N)(::Int64). How may I correct this script?

Thanks in advance.

Use T = typeof(a * b / 1) and Z = Array{T}(undef, obs)

2 Likes

Worked like a charm. Thank you very much!

You can also just let Julia create the array for you with the right type:

function test(a,b, obs)
    map(1:obs) do i
        a * b / i
    end
end
5 Likes