# Parametric Constructor in Julia 0.6

**URL:** https://discourse.julialang.org/t/parametric-constructor-in-julia-0-6/3652
**Category:** General Usage
**Created:** [May 11, 2017, 4:51pm UTC](https://discourse.julialang.org/t/parametric-constructor-in-julia-0-6/3652 "2017-05-11T16:51:01Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![matthieu](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@matthieu](https://discourse.julialang.org/u/matthieu)
#### Post date: [May 11, 2017, 4:51pm UTC](https://discourse.julialang.org/t/parametric-constructor-in-julia-0-6/3652/1 "2017-05-11T16:51:01Z")

</div>

I have a hard time defining a constructor for a parametric type

```nohighlight
struct Point{T}
   x::T
   y::T
   Point{T}(x,y) where {T} = new(x,y)
end

Point(0.0, 0.0)
# ERROR: MethodError: no method matching Point(::Float64, ::Float64)

```

What am I missing?

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [May 11, 2017, 5:11pm UTC](https://discourse.julialang.org/t/parametric-constructor-in-julia-0-6/3652/2 "2017-05-11T17:11:20Z")

</div>

The constructor you’ve defined (`Point{T}(x, y)`) is invoked when the `T` parameter is explicitly given, like this:

```julia
Point{Float64}(0.0, 0.0)

```

The reason this is happening is that by defining your own inner constructor, you’ve overwritten the default inner constructor for your type. You could fix this by defining a more flexible inner constructor:

```julia
struct Point{T}
  x::T
  y::T
  Point(x::T, y::T) where {T} = new{T}(x, y) # called with Point(x, y)
  Point{T}(x, y) where {T} = new{T}(x, y) # called with Point{T}(x, y)
end

julia> Point(0.0, 0.0)
Point{Float64}(0.0, 0.0)

julia> Point{Int}(0.0, 0.0)
Point{Int64}(0, 0)

```

---

<div class="post-metadata">

### Author: ![musm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/musm/32/3675_2.png) [@musm](https://discourse.julialang.org/u/musm)
#### Post date: [May 11, 2017, 5:19pm UTC](https://discourse.julialang.org/t/parametric-constructor-in-julia-0-6/3652/3 "2017-05-11T17:19:31Z")

</div>

Is there any advantage/difference between what you have posted and

```julia
struct Point{T}
  x::T
  y::T
  Point{T}(x, y) where {T} = new{T}(x, y) # called with Point{T}(x, y)
end
Point(x::T, y::T) where {T} = Point{T}(x, y) # called with Point(x, y)

```

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [May 11, 2017, 5:29pm UTC](https://discourse.julialang.org/t/parametric-constructor-in-julia-0-6/3652/4 "2017-05-11T17:29:53Z")

</div>

@musm actually I think your version is more standard, but I’m not super clear on the distinction.

---

<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: [May 11, 2017, 5:57pm UTC](https://discourse.julialang.org/t/parametric-constructor-in-julia-0-6/3652/5 "2017-05-11T17:57:55Z")

</div>

Same for dispatch. Putting it in the type def allows access to `new` keyword to construct the object without proxy though another constrictor

---

<div class="post-metadata">

### Author: ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)
#### Post date: [May 12, 2017, 8:16am UTC](https://discourse.julialang.org/t/parametric-constructor-in-julia-0-6/3652/6 "2017-05-12T08:16:28Z")

</div>

I’d prefer the inner constructor to avoid that tiny code repetition (`new` vs `Point`).

---

<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: [May 12, 2017, 9:00am UTC](https://discourse.julialang.org/t/parametric-constructor-in-julia-0-6/3652/7 "2017-05-12T09:00:22Z")

</div>

> [@yuyichao](#):
>
> construct the object without proxy though another constrictor

Wouldn’t the compiler inline this anyway?

From the perspective of coding style, inner constructors are generally justified when they provide something extra (incomplete initialization, checking consistency), in which case it may be better to pack the extra functionality in a few inner constructors (ideally one), and call that.

---

<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: [May 12, 2017, 7:31pm UTC](https://discourse.julialang.org/t/parametric-constructor-in-julia-0-6/3652/8 "2017-05-12T19:31:30Z")

</div>

Inlining isn’t the issue at all. Having access to `new` means that you are not constraint by the signature of defined constructors and can construct the object directly.
