# Problem: setting up parametric struct

**URL:** https://discourse.julialang.org/t/problem-setting-up-parametric-struct/57699
**Category:** New to Julia
**Created:** [March 22, 2021, 10:37am UTC](https://discourse.julialang.org/t/problem-setting-up-parametric-struct/57699 "2021-03-22T10:37:42Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ZettEff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zetteff/32/31976_2.png) [@ZettEff](https://discourse.julialang.org/u/ZettEff)
#### Post date: [March 22, 2021, 10:37am UTC](https://discourse.julialang.org/t/problem-setting-up-parametric-struct/57699/1 "2021-03-22T10:37:43Z")

</div>

I am struggling to correctly set up a parametric struct.

This is most likely a very stupid question, however, I really don’t understand what is wrong with my code so please be gentle on me…

I want to create a struct which has a field that can be of various types which all are subtypes of one abstract type.

I created the following dummy example which is not working.

Can anyone explain to me

1. How to fix this code and
2. Why the enum version further below is working

```julia
abstract type Bar end 
struct SpecificBar <: Bar end 

mutable struct Foo{T <: Bar}
    x::Array{Tuple}
    type::T
end

Foo([(1,1)], SpecificBar()) # returns error

```

I get the following error: `ERROR: LoadError: MethodError: no method matching Foo(::Array{Tuple{Int64,Int64},1}, ::SpecificBar) Closest candidates are: Foo(::Array{Tuple,N} where N, ::T) where T<:Bar`

My interpretation of the error message is that the issue is not with the field `type` but with the field `x`. If this is the case it is surprising to me that the below code using `enum` instead of abstract type works.

```julia
@enum BarEnum Bar1 Bar2

mutable struct Foo2
    x::Array{Tuple}
    type::BarEnum
end

Foo2([(1,1)], Bar1)

```

Given I want to use multiple dispatch I feel it is better to use an abstract type instead of enums. Is this even a valid argument?

Thanks for your help!

---

<div class="post-metadata">

### Author: ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)
#### Post date: [March 22, 2021, 10:50am UTC](https://discourse.julialang.org/t/problem-setting-up-parametric-struct/57699/2 "2021-03-22T10:50:01Z")

</div>

You’ve run into [this issue.](https://github.com/JuliaLang/julia/issues/35053). The issue is a missing default constructor for parametric types. Please comment on the issue so people can see this is something that actually bites people in the butt 🙂

(btw, the solution is to do `Foo{SpecificBar}([(1,1)], SpecificBar())`)

---

<div class="post-metadata">

### Author: ![ZettEff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zetteff/32/31976_2.png) [@ZettEff](https://discourse.julialang.org/u/ZettEff)
#### Post date: [March 22, 2021, 11:03am UTC](https://discourse.julialang.org/t/problem-setting-up-parametric-struct/57699/3 "2021-03-22T11:03:30Z")

</div>

Great, thanks for your swift reply. I am happy to comment. No expert in programming and obviously Julia but I really didn’t get what the problem was.

So I guess the following is a quick fix for my code then.

```julia
Foo(x, type::Bar) = Foo{typeof{type}}(x, type)

```

Thanks!

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [March 22, 2021, 11:55am UTC](https://discourse.julialang.org/t/problem-setting-up-parametric-struct/57699/4 "2021-03-22T11:55:42Z")

</div>

or

```julia
Foo(x, type::T) where T<:Bar = Foo{T}(x,type)

```

(the `<:Bar` is not needed for this to work, but with this construct you have an exact association with the struct definition).
