# Unexpected ambigiuity in subtype constructor

**URL:** https://discourse.julialang.org/t/unexpected-ambigiuity-in-subtype-constructor/80475
**Category:** General Usage
**Tags:** question, parametric-methods, constructors
**Created:** [May 4, 2022, 11:31am UTC](https://discourse.julialang.org/t/unexpected-ambigiuity-in-subtype-constructor/80475 "2022-05-04T11:31:27Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![sjrodahl](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@sjrodahl](https://discourse.julialang.org/u/sjrodahl)
#### Post date: [May 4, 2022, 11:31am UTC](https://discourse.julialang.org/t/unexpected-ambigiuity-in-subtype-constructor/80475/1 "2022-05-04T11:31:27Z")

</div>

Consider this example:

```julia
julia> abstract type AbstractT end
julia> struct A <: AbstractT
           i::Int
       end
julia> struct Param
           myparam::Int
       end
julia> (::Type{T})(p::Param) where T<:AbstractT = T(p.myparam)
julia> A(Param(4))
ERROR: MethodError: A(::Param) is ambiguous. Candidates:
  (::Type{T})(p::Param) where T<:AbstractT in Main at REPL[4]:1
  A(i) in Main at REPL[2]:2
Possible fix, define
  A(::Param)
Stacktrace:
 [1] top-level scope
   @ REPL[5]:1

```

Why is `(::Type{T})(p::Param)` conflicting with `A(::Any)`? I would expect it to be chosen as it is more specified.

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [May 4, 2022, 11:39am UTC](https://discourse.julialang.org/t/unexpected-ambigiuity-in-subtype-constructor/80475/2 "2022-05-04T11:39:18Z")

</div>

In `A(i)`, `A` is an exact type (you could write the signature as `(::Type{T})(p::Param) where A<:T<:A`), but the argument is `<:Any`. The opposite is true for `(::Type{T})(p::Param) where T<:AbstractT`.  
You could re-write the problem as

```julia
julia> f(::Type{T}, p::Param) where T<:AbstractT = 1
f (generic function with 1 method)

julia> f(::Type{A}, p) = 2
f (generic function with 2 methods)

julia> f(A, Param(2))
ERROR: MethodError: f(::Type{A}, ::Param) is ambiguous. Candidates:
  f(::Type{T}, p::Param) where T<:AbstractT in Main at REPL[73]:1
  f(::Type{A}, p) in Main at REPL[74]:1

```

which hopefully makes the ambiguity more obvious.

---

<div class="post-metadata">

### Author: ![sjrodahl](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@sjrodahl](https://discourse.julialang.org/u/sjrodahl)
#### Post date: [May 4, 2022, 12:21pm UTC](https://discourse.julialang.org/t/unexpected-ambigiuity-in-subtype-constructor/80475/3 "2022-05-04T12:21:00Z")

</div>

Thank you, that helps clear up my confusion.

One option to avoid ambiguity then is to specify an inner constructor, so the `::Any`-method is never created:

```julia
julia> struct B <: AbstractT
           i::Int
           B(i::Int) = new(i)
       end
julia> B(Param(4))
B(4)

```
