# Ways of using constructor of a parametric type

**URL:** https://discourse.julialang.org/t/ways-of-using-constructor-of-a-parametric-type/5944
**Category:** General Usage
**Tags:** parametric-types, constructors
**Created:** [September 18, 2017, 11:57am UTC](https://discourse.julialang.org/t/ways-of-using-constructor-of-a-parametric-type/5944 "2017-09-18T11:57:04Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![julbinb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/julbinb/32/3079_2.png) [@julbinb](https://discourse.julialang.org/u/julbinb)
#### Post date: [September 18, 2017, 11:57am UTC](https://discourse.julialang.org/t/ways-of-using-constructor-of-a-parametric-type/5944/1 "2017-09-18T11:57:04Z")

</div>

I noticed a strange behavior when using constructors of parametric types.

If a constructor does not have constraints on type parameters, then both type inference and providing explicit type argument work:

```julia
struct Foo{T}
  val :: T
  Foo(x :: T) where T = new{T}(x)
end

julia> Foo(3)
Foo{Int64}(3)

julia> Foo{Number}(3)
Foo{Number}(3)

```

But if I add a constraint on a type parameter, then depending on the syntax of a constructor only one of the constructor’s calls works:

```julia
struct Bar{T}
  val :: T
  Bar(x :: T) where T <: Number = new{T}(x)
end

struct Baz{T}
  val :: T
  Baz{T}(x :: T) where T <: Number = new{T}(x)
end

julia> Bar(2)
Bar{Int64}(2)

julia> Bar{Number}(2)
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type Bar{Number}
This may have arisen from a call to the constructor Bar{Number}(...),
since type constructors fall back to convert methods.
Stacktrace:
 [1] Bar{Number}(::Int64) at ./sysimg.jl:102

julia> Baz{Number}(2)
Baz{Number}(2)

julia> Baz(2)
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type Baz
This may have arisen from a call to the constructor Baz(...),
since type constructors fall back to convert methods.
Stacktrace:
 [1] Baz(::Int64) at ./sysimg.jl:102

```

Is this a bug?

---

<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: [September 18, 2017, 12:10pm UTC](https://discourse.julialang.org/t/ways-of-using-constructor-of-a-parametric-type/5944/2 "2017-09-18T12:10:14Z")

</div>

Nope, this is intended behavior. You are defining `Bar(...)` and `Baz{T}(...) where T` functions (which happen to be constructors), but not the other ones you are trying to call. See

> [@Anybody else find the new where syntax less than satisfactory?](https://discourse.julialang.org/t/anybody-else-find-the-new-where-syntax-less-than-satisfactory/4897/14):
>
> I explained the overall motivation for the where syntax in this StackOverflow answer: Partly quoting from there (with some clarifications and edits): Fundamentally, the problem with F{T}(args...) in Julia 0.5 and earlier is that the F{T} part is ambiguous – the parser knows what it means from the broader context, but its meaning depends heavily on that context. The fact that it can mean such different things is pretty confusing: In most contexts, F{T} means the parametric type F with type…

Also, `Foo{Number}(3)` does not work with your code in 0.6. Could it be the result of earlier experimentation?

---

<div class="post-metadata">

### Author: ![julbinb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/julbinb/32/3079_2.png) [@julbinb](https://discourse.julialang.org/u/julbinb)
#### Post date: [September 25, 2017, 1:08pm UTC](https://discourse.julialang.org/t/ways-of-using-constructor-of-a-parametric-type/5944/3 "2017-09-25T13:08:57Z")

</div>

Thank you for reply!

> [@Tamas\_Papp](#):
>
> Also, Foo{Number}(3) does not work with your code in 0.6. Could it be the result of earlier experimentation?

This is 0.7.0.
