# Using parametric type inside outer parametric constructor

**URL:** <https://discourse.julialang.org/t/using-parametric-type-inside-outer-parametric-constructor/53388>\
**Category:** General Usage\
**Tags:** linearalgebra, parametric-types, potential-bug\
**Created:** [January 15, 2021, 3:37pm UTC](https://discourse.julialang.org/t/using-parametric-type-inside-outer-parametric-constructor/53388 "2021-01-15T15:37:33Z")\
**Posts on this page:** 1\
**Showing post:** 28

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [January 15, 2021, 11:55pm UTC](https://discourse.julialang.org/t/using-parametric-type-inside-outer-parametric-constructor/53388/28 "2021-01-15T23:55:39Z")

</div>

My mistake, I copied the code and forgot to change the commented lines.

It seems clear to me that what [I pointed](https://discourse.julialang.org/t/using-parametric-type-inside-outer-parametric-constructor/53388/15) in [your original MWE](https://discourse.julialang.org/t/using-parametric-type-inside-outer-parametric-constructor/53388/6) cannot be _exactly_ same that affected [OP’s MWE](https://discourse.julialang.org/t/using-parametric-type-inside-outer-parametric-constructor/53388/15). In OP’s MWE the problem happens because somehow in the first function called `T` is not recognized within the body of the first function called.

@schneiderfelipe Than for your kind words, but while I think I got it right for @lmiq MWE, I do not think I solved your original problem.

Now… this is funny:

```julia
julia> struct Foo{T<:Real, S<:AbstractMatrix{<:T}}
               data::S
               n::Int
       
               function Foo{T, S}(data, n) where {T<:Real, S<:AbstractMatrix{<:T}}
                   println("Foo{T, S}(data, n)")
                   Base.require_one_based_indexing(data)
                   new{T, S}(data, n)
               end
       end
       
       function Foo(A::AbstractMatrix{T}, n::Int) where {T<:Real}
           println("Foo(A, n)")
           return Foo{T, typeof(A)}(A, n)
       end
       
       # Changed the signature below to take both T and S even if S is not used
       function Foo{T, S}(::UndefInitializer, n::Int) where {T<:Real, S <: AbstractMatrix{<:T}}
           println("Foo{T, S}(undef, n)")
           d = fld(n * (n + 1), 2)
       
           # The next two lines should substitute each other,
           # but the commented one throws an error.
       
           A = Matrix{T}(undef, d, d) # UndefVarError: T not defined
           # A = zeros(d, d)
       
           return Foo(A, n)
       end

julia> Foo{Float64, Matrix{Float64}}(undef, 2)
Foo{T, S}(undef, n)
Foo(A, n)
Foo{T, S}(data, n)
Foo{Float64,Array{Float64,2}}([0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0], 2)

```

Why? Why the hell?

My guess now is that, maybe, just maybe, we are not allowed to define an outer constructor taking less type parameters than the `struct` itself?

---

_[View the full topic](https://discourse.julialang.org/t/using-parametric-type-inside-outer-parametric-constructor/53388)._
