# Get generic constructor of parametric type

**URL:** https://discourse.julialang.org/t/get-generic-constructor-of-parametric-type/57189
**Category:** New to Julia
**Tags:** question, parametric-types, constructors
**Created:** [March 15, 2021, 11:41am UTC](https://discourse.julialang.org/t/get-generic-constructor-of-parametric-type/57189 "2021-03-15T11:41:57Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)
#### Post date: [March 15, 2021, 11:41am UTC](https://discourse.julialang.org/t/get-generic-constructor-of-parametric-type/57189/1 "2021-03-15T11:41:57Z")

</div>

Is there a smart /easy way to get the generic constructor of a parametric type from its instance? I actually thought that this is a standard thing, but couldn’t find anything.

```julia
abstract type AbstractFooBar end
struct Foo{T} <: AbstractFooBar
    p::T
end
struct Bar{T} <: AbstractFooBar
    p::T
end
#= more subtypes
struct X{T} <: AbstractFooBar end
struct Y{T} <: AbstractFooBar end
.
=#

function get_generic_constructor(fb::AbstractFooBar)
    fb isa Bar && return Bar
    fb isa Foo && return Foo
    #= Would like to avoid doing this for all subtybes of AbstractFooBar
    fb isa X && return X ...
    =#
end

```

What I want to archive is the following behavior:

```julia
a = Bar(3) 
#Bar{Int64}(3)
b = get_generic_constructor(Bar(3))
#Bar
b("three") 
#Bar{String}("three")

```

I need this in my code to make it compatible to ForwardDiff.jl. So please my favorite community, enlighten me

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [March 15, 2021, 11:59am UTC](https://discourse.julialang.org/t/get-generic-constructor-of-parametric-type/57189/2 "2021-03-15T11:59:05Z")

</div>

Another way of saying what you want to do is to get the UnionAll type if it exists. You can get it with `typeof(a).name.wrapper`.  
That line can also take types without parameters, but in those cases, `typeof(a) === typeof(a).name.wrapper`

---

<div class="post-metadata">

### Author: ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)
#### Post date: [March 15, 2021, 12:13pm UTC](https://discourse.julialang.org/t/get-generic-constructor-of-parametric-type/57189/3 "2021-03-15T12:13:52Z")

</div>

Thanks,  
`typeof(a).name.wrapper` was what I was looking for.  
Since I never saw this anywhere before and I encountered this problem already multiple times, I wonder if I do something fundamentally wrong. However thanks a lot…

---

<div class="post-metadata">

### Author: ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)
#### Post date: [August 8, 2021, 11:49am UTC](https://discourse.julialang.org/t/get-generic-constructor-of-parametric-type/57189/4 "2021-08-08T11:49:08Z")

</div>

Great, I had the same question and found the answer here. Whatever you do fundamentally, wrong, so do I !!!

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [August 8, 2021, 5:03pm UTC](https://discourse.julialang.org/t/get-generic-constructor-of-parametric-type/57189/5 "2021-08-08T17:03:53Z")

</div>

> [@Benny](#):
>
> `typeof(a).name.wrapper`

I think this solution depends on implementation details and, therefore, may break in any change of Julia versions. As far as I know, the founders/‘language designers’ have not decided on a way to do what you want to do, or even if having such feature is desirable.

---

<div class="post-metadata">

### Author: ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)
#### Post date: [August 8, 2021, 5:47pm UTC](https://discourse.julialang.org/t/get-generic-constructor-of-parametric-type/57189/6 "2021-08-08T17:47:34Z")

</div>

Do you know if there is an open issue about that somewhere?. Because I can think of several workarounds for this, but they all suffer from a lack of generically and I would like to know more about the pros and cons regarding such a feature.

---

<div class="post-metadata">

### Author: ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)
#### Post date: [August 8, 2021, 6:04pm UTC](https://discourse.julialang.org/t/get-generic-constructor-of-parametric-type/57189/7 "2021-08-08T18:04:40Z")

</div>

Is this helpful [https://github.com/JuliaObjects/ConstructionBase.jl/blob/master/src/constructorof.md](https://github.com/JuliaObjects/ConstructionBase.jl/blob/master/src/constructorof.md)?

---

<div class="post-metadata">

### Author: ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)
#### Post date: [August 8, 2021, 6:26pm UTC](https://discourse.julialang.org/t/get-generic-constructor-of-parametric-type/57189/8 "2021-08-08T18:26:31Z")

</div>

Absolutely!! The whole magic is actually done only by these lines:

```julia
@generated function constructorof(::Type{T}) where T
    getfield(parentmodule(T), nameof(T))
end

constructorof(typeof(a)) # Bar

```

---

<div class="post-metadata">

### Author: ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)
#### Post date: [August 8, 2021, 7:58pm UTC](https://discourse.julialang.org/t/get-generic-constructor-of-parametric-type/57189/9 "2021-08-08T19:58:09Z")

</div>

> [@MatFi](#):
>
> Do you know if there is an open issue

I think you want [issue 35543](https://github.com/JuliaLang/julia/issues/35543).
