# How to call constructor of parametric family of types efficiently

**URL:** https://discourse.julialang.org/t/how-to-call-constructor-of-parametric-family-of-types-efficiently/38503
**Category:** Performance
**Created:** [April 30, 2020, 7:20pm UTC](https://discourse.julialang.org/t/how-to-call-constructor-of-parametric-family-of-types-efficiently/38503 "2020-04-30T19:20:06Z")
**Posts on this page:** 1
**Showing post:** 5

<div class="post-metadata">

### Author: ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)
#### Post date: [April 30, 2020, 8:26pm UTC](https://discourse.julialang.org/t/how-to-call-constructor-of-parametric-family-of-types-efficiently/38503/5 "2020-04-30T20:26:35Z")

</div>

This is a relatively hard problem, see e.g. the discussion on [Stripping parameter from parametric types](https://discourse.julialang.org/t/stripping-parameter-from-parametric-types/8293) here on discourse, or the paragraph on [Design Patterns with Parametric Methods](https://docs.julialang.org/en/latest/manual/methods/#Building-a-similar-type-with-a-different-type-parameter-1) in the documentation.

  

> [@SEA](#):
>
> I am trying to avoid having to write a separate method for each family.

I would think that building a separate method for each family remains the most idiomatic solution. This is more or less what [similar](https://docs.julialang.org/en/latest/base/arrays/#Base.similar) does, for example.

  

* * *
  

That being said, if you really want a fully generic implementation, something like this might work:

```julia
stripped_type(x) = stripped_type(typeof(x))
stripped_type(typ::DataType) = Base.typename(typ).wrapper

```

For example:

```julia
julia> struct A{T}
           x::T
       end

julia> a = A(42)
A{Int64}(42)

julia> stripped_type(a)(42.5)
A{Float64}(42.5)

```

Again, I would not recommend such a solution, which relies too much on internal details to my taste.

---

_[View the full topic](https://discourse.julialang.org/t/how-to-call-constructor-of-parametric-family-of-types-efficiently/38503)._
