# Outer constructor

**URL:** <https://discourse.julialang.org/t/outer-constructor/38086>\
**Category:** New to Julia\
**Created:** [April 23, 2020, 1:39pm UTC](https://discourse.julialang.org/t/outer-constructor/38086 "2020-04-23T13:39:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![QUANG\_VU\_NGUY\_N\_PH\_M](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quang_vu_nguy_n_ph_m/32/14299_2.png) [@QUANG\_VU\_NGUY\_N\_PH\_M](https://discourse.julialang.org/u/QUANG_VU_NGUY_N_PH_M)\
**Post date:** [April 23, 2020, 1:39pm UTC](https://discourse.julialang.org/t/outer-constructor/38086/1 "2020-04-23T13:39:53Z")

</div>

My struct definition:

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

```

I would like to create an outer constructor:

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

```julia
DiscreteField(data)

```

Error message:

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

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

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 23, 2020, 2:00pm UTC](https://discourse.julialang.org/t/outer-constructor/38086/2 "2020-04-23T14:00:02Z")

</div>

> [@QUANG\_VU\_NGUY\_N\_PH\_M](#):
>
> Would you mind telling me what was the error in the previous constructor?

Nothing _per se_, but you probably called it with a `data` it didn’t have a method for. Since you did not provide `data`, it’s hard to say what the problem was.

Generally, it is a very bad idea to specialize constructors like this. `Base.Iterators.Zip` is an implementation detail of `zip`, and could change any time. Just defining `DiscreteField(data)` would be fine, and it would work on any iterable that provides 2-tuples. See the last point in this recent blog post:

[https://white.ucc.asn.au/2020/04/19/Julia-Antipatterns.html](https://white.ucc.asn.au/2020/04/19/Julia-Antipatterns.html)

---

<div class="post-metadata">

**Author:** ![QUANG\_VU\_NGUY\_N\_PH\_M](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quang_vu_nguy_n_ph_m/32/14299_2.png) [@QUANG\_VU\_NGUY\_N\_PH\_M](https://discourse.julialang.org/u/QUANG_VU_NGUY_N_PH_M)\
**Post date:** [April 23, 2020, 2:24pm UTC](https://discourse.julialang.org/t/outer-constructor/38086/3 "2020-04-23T14:24:19Z")

</div>

> [@Tamas\_Papp](#):
>
> pecialize constructors like this. `Base.Iterators.Zip` is an implementation detail of `zip` , and could change any time. Just defining `DiscreteField(data)` would be fine, and it would work on any iterable

Thank you for your advice! I got that idea and success with tuple.
