# Better way to get the corresponding constructor from composite-type instances?

**URL:** https://discourse.julialang.org/t/better-way-to-get-the-corresponding-constructor-from-composite-type-instances/122661
**Category:** General Usage
**Tags:** parametric-types, constructors
**Created:** [November 15, 2024, 12:09am UTC](https://discourse.julialang.org/t/better-way-to-get-the-corresponding-constructor-from-composite-type-instances/122661 "2024-11-15T00:09:57Z")
**Posts on this page:** 1
**Showing post:** 5

<div class="post-metadata">

### Author: ![JonasWickman](https://avatars.discourse-cdn.com/v4/letter/j/9de0a6/32.png) [@JonasWickman](https://discourse.julialang.org/u/JonasWickman)
#### Post date: [November 15, 2024, 3:27pm UTC](https://discourse.julialang.org/t/better-way-to-get-the-corresponding-constructor-from-composite-type-instances/122661/5 "2024-11-15T15:27:25Z")

</div>

This was discussed a little while ago in this thread: [Is there a way to get a UnionAll base type from a concrete type?](https://discourse.julialang.org/t/is-there-a-way-to-get-a-unionall-base-type-from-a-concrete-type/122328)

One good way seems to be to use the package ConstructionBase.jl, and write

```julia
using ConstructionBase
m1 = myT1(1.0)
constructorof(typeof(m1)) # myT1

```

It’s not defined for instances, but you could define

```julia
builder(x::Type) = constructorof(x)
builder(x) = builder(typeof(x))

```

---

_[View the full topic](https://discourse.julialang.org/t/better-way-to-get-the-corresponding-constructor-from-composite-type-instances/122661)._
