# Why is convert not called for parametric type?

**URL:** <https://discourse.julialang.org/t/why-is-convert-not-called-for-parametric-type/25376>\
**Category:** General Usage\
**Tags:** parametric-types, convert\
**Created:** [June 17, 2019, 7:23pm UTC](https://discourse.julialang.org/t/why-is-convert-not-called-for-parametric-type/25376 "2019-06-17T19:23:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [June 17, 2019, 7:23pm UTC](https://discourse.julialang.org/t/why-is-convert-not-called-for-parametric-type/25376/1 "2019-06-17T19:23:33Z")

</div>

For parametric type, does the constructor not call `convert` automatically?

```julia
julia> struct Foo3{T}
           a::T
           b::Float64
       end

julia> Foo3(1,2)
ERROR: MethodError: no method matching Foo3(::Int64, ::Int64)
Closest candidates are:
  Foo3(::T, ::Float64) where T at REPL[25]:2
Stacktrace:
 [1] top-level scope at none:0

julia> Foo3(1,2.0)
Foo3{Int64}(1, 2.0)

```

It works properly for regular types:

```julia
julia> struct Foo2
           x::Int
           y::Float64
       end

julia> Foo2(1,2)
Foo2(1, 2.0)

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 17, 2019, 7:31pm UTC](https://discourse.julialang.org/t/why-is-convert-not-called-for-parametric-type/25376/2 "2019-06-17T19:31:51Z")

</div>

The constructor **DOES** call convert.

```julia
julia> Foo3{Int}(1, 2)
Foo3{Int64}(1, 2.0)

```

You can see clearly from the error message and the backtrace that the error happens **on** the call of `Foo3`, not inside the constructor. There simply isn’t a constructor defined in this case to guess the type parameter for you.

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [June 17, 2019, 7:39pm UTC](https://discourse.julialang.org/t/why-is-convert-not-called-for-parametric-type/25376/3 "2019-06-17T19:39:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [June 17, 2019, 10:18pm UTC](https://discourse.julialang.org/t/why-is-convert-not-called-for-parametric-type/25376/4 "2019-06-17T22:18:39Z")

</div>

Yes, it would be nice if it just works without having to specify the type parameter.

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [June 18, 2019, 5:39am UTC](https://discourse.julialang.org/t/why-is-convert-not-called-for-parametric-type/25376/5 "2019-06-18T05:39:30Z")

</div>

You could manually define an outer constructor though:  
`Foo3(a::T, b) where {T} = Foo3{T}(a, b)`

---

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [June 20, 2019, 6:29am UTC](https://discourse.julialang.org/t/why-is-convert-not-called-for-parametric-type/25376/6 "2019-06-20T06:29:07Z")

</div>

That’s a good idea! If such outer constructor were “auto-generated” then it would be super-awesome.
