Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

Outer constructor

$
0
0

@QUANG_VU_NGUY_N_PH_M wrote:

My struct definition:

struct DiscreteField{T}
    time::Vector{Float64}
    data::Vector{T}   
end

I would like to create an outer constructor:

function DiscreteField(data::Base.Iterators.Zip{Tuple{Vector{Float64},Vector{}}})
    a = Float64[]
    b = []

    for (time, val) in data
        push!(a, time)
        push!(b, val)
    end
    return DiscreteField(a, b)
end

Then the initialization of object got error:

DiscreteField(data)

Error message:

MethodError: no method matching DiscreteField(::Base.Iterators.Zip{Tuple{Array{Float64,1},Array{Int64,1}}})
Closest candidates are:
  DiscreteField() at In[8]:7
  DiscreteField(!Matched::Array{Float64,1}, !Matched::Array{T,1}) where T at In[11]:4
  DiscreteField(!Matched::Base.Iterators.Zip{Tuple{Array{Float64,1},Array{T,1} where T}}) at In[15]:2

Stacktrace:
 [1] top-level scope at In[17]:100:

Why does this happens?

I have the other outer constructor which works well:

function DiscreteField{T}(data::Base.Iterators.Zip{Tuple{Vector{Float64}, Vector{T}}}) where {T}
    a = Float64[]
    b = []

    for (time, val) in data
        push!(a, time)
        push!(b, val)
    end
    return DiscreteField(a, b)
end

Would you mind telling me what was the error in the previous constructor?

Posts: 3

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 2795

Latest Images

Trending Articles



Latest Images