# Struct & inner constructor

**URL:** <https://discourse.julialang.org/t/struct-inner-constructor/37971>\
**Category:** New to Julia\
**Created:** [April 21, 2020, 3:17pm UTC](https://discourse.julialang.org/t/struct-inner-constructor/37971 "2020-04-21T15:17:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [April 21, 2020, 3:17pm UTC](https://discourse.julialang.org/t/struct-inner-constructor/37971/1 "2020-04-21T15:17:24Z")

</div>

I’m trying to create a `struct` with an inner constructor to check some dimensions… Basic structure (let’s call it `TEST`) works:

```julia
using Polynomials
#
struct TEST{T}
  N::Matrix{Poly{T}}
  D::Matrix{Poly{T}}
end

```

This works (although I could introduce a `show` method):

```julia
julia> num = Poly.(fill([1,1],2,3));
julia> den = Poly.fill([1,1],2,2));
julia> TEST(num,den)
TEST{Int64}(Poly{Int64}[Poly(1 + x) Poly(1 + x) Poly(1 + x); Poly(1 + x) Poly(1 + x) Poly(1 + x)], Poly{Int64}[Poly(1 + 2*x + x^2) Poly(1 + 2*x + x^2); Poly(1 + 2*x + x^2) Poly(1 + 2*x + x^2)])

```

Next, I’d like to insert a test of dimensions of `N` and `D` to make sure that the description is correct/that `N` and `D` are compatible. I’m trying with the following:

```julia
struct TEST1{T}
    N::Matrix{Poly{T}}
    D::Matrix{Poly{T}}
    #
    function TEST1{T}(N::Matrix{Poly{T}},D::Matrix{Poly{T}}) where T <: Number
        n1,n2 = size(N)
        d1,d2 = size(D)
        if d1 != d2
            error("D matrix must be square")
        end
        if (n2 != d1) && (d2 != n1)
            error("N,D dimensions are incompatible")
        end
        new{T}(N,D)
    end
    #
end

```

and then get an error message during construction:

```julia
julia> TEST1(num,den)
MethodError: no method matching TEST1(::Array{Poly{Int64},2}, ::Array{Poly{Int64},2})

Stacktrace:
 [1] top-level scope at In[49]:1

```

This is my first attempt of a `struct`, so I assume the solution is trivial, but…  
**Questions**

1. What do I do wrong?
2. What is the simplest inner constructor to achieve what I want?

---

<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 21, 2020, 3:22pm UTC](https://discourse.julialang.org/t/struct-inner-constructor/37971/2 "2020-04-21T15:22:10Z")

</div>

You would need to call `TEST1{T}(...)` (with an explicit `T`).

One solution is defining a method that dispatches to this, eg

```julia
TEST1(N::Matrix{Poly{T}},D::Matrix{Poly{T}}) where T = TEST1{T}(N, D)

```

the other one is defining your inner constructor without the first `{T}`. I would suggest the latter if you get the `T` anyway from the arguments.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [April 21, 2020, 4:07pm UTC](https://discourse.julialang.org/t/struct-inner-constructor/37971/3 "2020-04-21T16:07:16Z")

</div>

Thanks! The following seems to work…

```julia
struct TEST1{T}
    N::Matrix{Poly{T}}
    D::Matrix{Poly{T}}
    #
    function TEST1(N::Matrix{Poly{T}},D::Matrix{Poly{T}}) where T <: Number
        n1,n2 = size(N)
        d1,d2 = size(D)
        if d1 != d2
            error("D matrix must be square")
        end
        if (n2 != d1) && (d2 != n1)
            error("N,D dimensions are incompatible")
        end
        new{T}(N,D)
    end
    #
end

```

I’ll check it a little bit more, though.
