# Generating automatic constructors for parametric type

**URL:** https://discourse.julialang.org/t/generating-automatic-constructors-for-parametric-type/2963
**Category:** General Usage
**Tags:** question, parametric-types, constructors
**Created:** [March 30, 2017, 9:31am UTC](https://discourse.julialang.org/t/generating-automatic-constructors-for-parametric-type/2963 "2017-03-30T09:31:24Z")
**Posts on this page:** 8
**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: [March 30, 2017, 9:31am UTC](https://discourse.julialang.org/t/generating-automatic-constructors-for-parametric-type/2963/1 "2017-03-30T09:31:24Z")

</div>

MWE: for a parametric type definition with an inner constructor

```julia
struct Foo{T <: Real}
    x::T
    function Foo{T}(x) where T
        @assert x > 0
        new(x)
    end
end

```

I frequently end up writing boilerplate

```julia
Foo(x::T) where T = Foo{T}(x)

```

Is there a construct in `Base` which would allow me to get these for free? Or perhaps a macro in a package?

(incidentally, is the above the new `v0.6` syntax? I am still confused about which `<:` becomes a `where`.)

---

<div class="post-metadata">

### Author: ![Evizero](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evizero/32/10118_2.png) [@Evizero](https://discourse.julialang.org/u/Evizero)
#### Post date: [March 30, 2017, 9:57am UTC](https://discourse.julialang.org/t/generating-automatic-constructors-for-parametric-type/2963/2 "2017-03-30T09:57:30Z")

</div>

Slightly off topic, but related (somewhat)

> [@Tamas\_Papp](#):
>
> Foo(x::T) where T = Foo{T}(x)

I parse these kinds of single line function definitions wrong _all the time_ in my head.  
Will the syntax `Foo{T}(X::T) = Foo{T}(x)` continue to work in 1.0? it seems to still work in 0.6

---

<div class="post-metadata">

### Author: ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)
#### Post date: [March 30, 2017, 10:36am UTC](https://discourse.julialang.org/t/generating-automatic-constructors-for-parametric-type/2963/3 "2017-03-30T10:36:16Z")

</div>

`Foo(x::T) where {T} = Foo{T}(x)` looks better.

---

<div class="post-metadata">

### Author: ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)
#### Post date: [March 30, 2017, 10:46am UTC](https://discourse.julialang.org/t/generating-automatic-constructors-for-parametric-type/2963/4 "2017-03-30T10:46:10Z")

</div>

The exact “boilerplate” constructor you mentioned is provided for free whenever no inner constructor is defined.

AFAICT if you need an inner constructor (for an invariant, say) then it is assumed you are an advanced user doing something fancy, and the default outer constructor could potentially get in your way (remember while you can define a method, you can’t really “undefined” one, so as unfortunate as it is I’m not sure a better choice is available).

---

<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: [March 30, 2017, 1:25pm UTC](https://discourse.julialang.org/t/generating-automatic-constructors-for-parametric-type/2963/5 "2017-03-30T13:25:48Z")

</div>

@andyferris: I am afraid you misunderstand the question. The issue is not whether one is an “advanced user” who can write this, but that one is repeating code unnecessarily.

It is easy to write a macro for this, but the problem is that AFAICT those kinds of macros (eg `Parameters.@with_kw`, `@auto_hash_equals`) cannot be combined. Maybe a meta-macro

```julia
@struct_meta{with_kw, auto_has_equals, parametric_constructor} struct Foo
    ...
end

```

could be the solution, where each expander gets the form `struct Foo end` and spits out extra code which is then combined.

---

<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: [March 31, 2017, 12:35am UTC](https://discourse.julialang.org/t/generating-automatic-constructors-for-parametric-type/2963/6 "2017-03-31T00:35:03Z")

</div>

Using [QuickTypes.jl](https://github.com/cstjean/QuickTypes.jl)

```julia
using QuickTypes

@qstruct Foo{T <: Real}(x::T) do
    @assert x > 0
end

```

It defines both the inner and outer constructors.

---

<div class="post-metadata">

### Author: ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)
#### Post date: [April 27, 2017, 6:33pm UTC](https://discourse.julialang.org/t/generating-automatic-constructors-for-parametric-type/2963/7 "2017-04-27T18:33:45Z")

</div>

Was there ever a good reply for why defining an inner constructor removes the automatic nonparametrized “boilerplate” methods?

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [April 27, 2017, 11:46pm UTC](https://discourse.julialang.org/t/generating-automatic-constructors-for-parametric-type/2963/8 "2017-04-27T23:46:40Z")

</div>

That allows one to create a type and determine all behaviors. If the default methods were made to exist (invisibly) alongside the specialty type, unexpected interactions can (and do) occur.
