# Repeated type parameter in method error of promoting constructor

**URL:** https://discourse.julialang.org/t/repeated-type-parameter-in-method-error-of-promoting-constructor/34892
**Category:** General Usage
**Tags:** question
**Created:** [February 20, 2020, 8:37am UTC](https://discourse.julialang.org/t/repeated-type-parameter-in-method-error-of-promoting-constructor/34892 "2020-02-20T08:37:26Z")
**Posts on this page:** 6
**Page:** 1

<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: [February 20, 2020, 8:37am UTC](https://discourse.julialang.org/t/repeated-type-parameter-in-method-error-of-promoting-constructor/34892/1 "2020-02-20T08:37:26Z")

</div>

Consider the type with the promoting constructor

```julia
struct Foo{T,TX<:AbstractVector{T},TFX}
    Δ::T
    x::TX
    fx::TFX
    residual_norm::T
    converged::Bool
    iterations::Int
end

function Foo(Δ::T1, x::AbstractVector{T2}, fx, residual_norm::T3, converged,
                           iterations) where {T1 <: Real, T2 <: Real, T3 <: Real}
    T = promote_type(T1, T2, T3)
    Foo(T(Δ), T.(x), fx, T(residual_norm), converged, iterations)
end

```

but

```julia
julia> Foo(1, [1.0, 2.0], nothing, 1.0, true, 0)
ERROR: MethodError: Foo(::Float64, ::Array{Float64,1}, ::Nothing, ::Float64, ::Bool, ::Int64) is ambiguous. Candidates:
  (::Type{Foo})(Δ::T, x::TX, fx::TFX, residual_norm::T, converged::Bool, iterations::Int64) where {T, TX<:AbstractArray{T,1}, TFX} in Main at REPL[1]:2
  (::Type{Foo})(Δ::T1, x::AbstractArray{T2,1}, fx, residual_norm::T3, converged, iterations) where {T1<:Real, T2<:Real, T3<:Real} in Main at REPL[2]:3
Possible fix, define
  Foo(::T1, ::Union{TX, TX}, ::TFX, ::Union{T3, T}, ::Bool, ::Int64) where {T1<:Real, T2<:Real, T3<:T2, TX<:AbstractArray{T2,1}, T<:Real, TX<:AbstractArray{T,1}, TFX}
Stacktrace:
 [1] Foo(::Int64, ::Array{Float64,1}, ::Nothing, ::Float64, ::Bool, ::Int64) at ./REPL[2]:4
 [2] top-level scope at REPL[3]:1

```

The part I don’t get is

1. why doesn’t this work,
2. why the `::Union{TX, TX}`,
3. why the repeated `TX` type parameter.

Is this a bug? Using 1.4-rc1, but could reproduce on 1.3.

---

<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: [February 20, 2020, 8:58am UTC](https://discourse.julialang.org/t/repeated-type-parameter-in-method-error-of-promoting-constructor/34892/2 "2020-02-20T08:58:17Z")

</div>

Also, modifying the converting constructor as

```julia
function Foo(Δ::T1, x::AbstractVector{T2}, fx, residual_norm::T3, converged,
             iterations) where {T1 <: Real, T2 <: Real, T3 <: Real}
    T = promote_type(T1, T2, T3)
    Tx = T.(x)
    Foo{T,typeof(Tx)}(T(Δ), Tx, fx, T(residual_norm), converged, iterations)
end

```

gives

```julia
julia> Foo(1, [1.0, 2.0], nothing, 1.0, true, 0)
ERROR: MethodError: no method matching Foo{Float64,Array{Float64,1},TFX} where TFX(::Float64, ::Array{Float64,1}, ::Nothing, ::Float64, ::Bool, ::Int64)
Stacktrace:
 [1] Foo(::Int64, ::Array{Float64,1}, ::Nothing, ::Float64, ::Bool, ::Int64) at ./REPL[8]:5
 [2] top-level scope at REPL[9]:1

```

where what follows `where` is really unusual.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [February 20, 2020, 9:00am UTC](https://discourse.julialang.org/t/repeated-type-parameter-in-method-error-of-promoting-constructor/34892/3 "2020-02-20T09:00:41Z")

</div>

If you constrain `T` in the `struct` to also be `<:Real` it works. I am guessing your constraints in the outer constructors are stronger in some sense than the default constructor since they enforce `<:Real` but weaker since they don’t enforce `TX<:AbstractVector{T}` which seems like an ambiguity.

```julia
julia> methods(Foo)
# 2 methods for type constructor:
[1] (::Type{Foo})(Δ::T, x::TX, fx::TFX, residual_norm::T, converged::Bool, iterations::Int64) where {T, TX<:AbstractArray{T,1}, TFX} in Main at REPL[2]:2
[2] (::Type{Foo})(Δ::T1, x::AbstractArray{T2,1}, fx, residual_norm::T3, converged, iterations) where {T1<:Real, T2<:Real, T3<:Real} in Main at REPL[4]:3

```

---

<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: [February 20, 2020, 9:09am UTC](https://discourse.julialang.org/t/repeated-type-parameter-in-method-error-of-promoting-constructor/34892/4 "2020-02-20T09:09:31Z")

</div>

do you think that the

> [@Tamas\_Papp](#):
>
> `TX<:AbstractArray{T2,1}, T<:Real, TX<:AbstractArray{T,1}`

should be reported as an issue? also the `Union{TX,TX}` which I think is related.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [February 20, 2020, 11:12am UTC](https://discourse.julialang.org/t/repeated-type-parameter-in-method-error-of-promoting-constructor/34892/5 "2020-02-20T11:12:58Z")

</div>

Yeah, the second `where` error message looks weird to me.

---

<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: [February 20, 2020, 1:55pm UTC](https://discourse.julialang.org/t/repeated-type-parameter-in-method-error-of-promoting-constructor/34892/6 "2020-02-20T13:55:27Z")

</div>

Thanks, reported here:

[https://github.com/JuliaLang/julia/issues/34824](https://github.com/JuliaLang/julia/issues/34824)
