# Type Reconstruction Question

**URL:** <https://discourse.julialang.org/t/type-reconstruction-question/23092>\
**Category:** General Usage\
**Created:** [April 12, 2019, 6:38pm UTC](https://discourse.julialang.org/t/type-reconstruction-question/23092 "2019-04-12T18:38:40Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [April 12, 2019, 6:38pm UTC](https://discourse.julialang.org/t/type-reconstruction-question/23092/1 "2019-04-12T18:38:40Z")

</div>

Hi,

Let’s assume that I have the following definition

```nohighlight
julia>abstract type Foo{T,U} end

julia> typ = Foo{Int,Float64}
Foo{Int64,Float64}

julia> typ.parameters
svec(Int64, Float64)

#this is the function I would like to write
julia>reconstruct_params(typ,(Float64,Float64))
Foo{Float64,Float64}

```

How can I write the “reconstruct\_params” function? How can I manipulate types at runtime?

Thanks

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [April 12, 2019, 6:44pm UTC](https://discourse.julialang.org/t/type-reconstruction-question/23092/2 "2019-04-12T18:44:08Z")

</div>

I think this is the same as was just asked:

> [@Tricky parametric type dispatch](https://discourse.julialang.org/t/tricky-parametric-type-dispatch/23064):
>
> I would like to dispatch a method like this, where T gets the type \<: AbstractParticles and the type parameters end up in F and NT. function Base.union(p1::T{F,NT},p2::S{F,NS}) where T \<: AbstractParticles where S \<: AbstractParticles{F,NS} where F where NT where NS T{F,NT+NS}([p1.particles; p2.particles]) end This does not work and an error TypeError: in Type{...} expression, expected UnionAll, got TypeVar occurs. The following definition is accepted, but it makes T include the type par…

---

<div class="post-metadata">

**Author:** ![zgornel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zgornel/32/217487_2.png) [@zgornel](https://discourse.julialang.org/u/zgornel)\
**Post date:** [April 12, 2019, 10:30pm UTC](https://discourse.julialang.org/t/type-reconstruction-question/23092/3 "2019-04-12T22:30:26Z")

</div>

```julia
function reconstruct_params(::Type{T}, ::Type{U}, ::Type{V}) where {T,U,V}
    return T{U,V}
end

```

or,

```julia
@generated function reconstruct_params(::Type{T}, ::Type{U}, ::Type{V}) where {T,U,V}
    return :(T{U,V})
end

```

Both versions work with

```julia
reconstruct_params(Foo, Float64, Float64)

```

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [April 12, 2019, 10:59pm UTC](https://discourse.julialang.org/t/type-reconstruction-question/23092/4 "2019-04-12T22:59:56Z")

</div>

But not with

```julia
julia> reconstruct_params(Foo{Int,Int}, Float64, Float64)
ERROR: TypeError: in Type{...} expression, expected UnionAll, got Type{Foo{Int64,Int64}}
Stacktrace:
 [1] reconstruct_params(::Type{Foo{Int64,Int64}}, ::Type{Float64}, ::Type{Float64}) at ./REPL[1]:2
 [2] top-level scope at none:0

```

which is the OP’s question.

---

<div class="post-metadata">

**Author:** ![zgornel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zgornel/32/217487_2.png) [@zgornel](https://discourse.julialang.org/u/zgornel)\
**Post date:** [April 12, 2019, 11:13pm UTC](https://discourse.julialang.org/t/type-reconstruction-question/23092/5 "2019-04-12T23:13:54Z")

</div>

```julia
function reconstruct_params(::Type{Foo{S,T}}, ::Type{U}, ::Type{V}) where {S,T,U,V}
    return Foo{U,V}
end

```

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [April 12, 2019, 11:32pm UTC](https://discourse.julialang.org/t/type-reconstruction-question/23092/6 "2019-04-12T23:32:29Z")

</div>

(Note though that this doesn’t extent to subtypes of Foo, which is what above link is about.)

---

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [April 13, 2019, 1:08am UTC](https://discourse.julialang.org/t/type-reconstruction-question/23092/7 "2019-04-13T01:08:09Z")

</div>

What if I want to do this on general scale,  
What if I wouldn’t know the type and the number of parameters it is taking.

Thanks

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 13, 2019, 1:39am UTC](https://discourse.julialang.org/t/type-reconstruction-question/23092/8 "2019-04-13T01:39:34Z")

</div>

What actual problem are you trying to solve? Probably there is a better way to do it … it seems like you are fighting with the language to do something in a non-idiomatic way.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [April 13, 2019, 6:48am UTC](https://discourse.julialang.org/t/type-reconstruction-question/23092/9 "2019-04-13T06:48:35Z")

</div>

> [@Qiyamah](#):
>
> What if I want to do this on general scale,

That’s what’s discussed in the link I posted. Maybe read that and then report back.

---

<div class="post-metadata">

**Author:** ![zgornel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zgornel/32/217487_2.png) [@zgornel](https://discourse.julialang.org/u/zgornel)\
**Post date:** [April 13, 2019, 9:44am UTC](https://discourse.julialang.org/t/type-reconstruction-question/23092/10 "2019-04-13T09:44:08Z")

</div>

One can easily overload the function for that.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [April 13, 2019, 11:47am UTC](https://discourse.julialang.org/t/type-reconstruction-question/23092/11 "2019-04-13T11:47:07Z")

</div>

Sure, but in general you have to do it for each subtype, which is essentially what similar does for arrays (plus it also instantiates the type).

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [April 13, 2019, 2:28pm UTC](https://discourse.julialang.org/t/type-reconstruction-question/23092/12 "2019-04-13T14:28:21Z")

</div>

```julia
julia> abstract type Foo{T,U} end
julia> Base.@pure reconstruct_params(::Type{T}, ::Type{P1},::Type{P2}) where {T,P1,P2}=T.name.wrapper{P1,P2}
julia> typ = Foo{String, Int}
Foo{String,Int64}
julia> reconstruct_params(typ, UInt8, Float32)
Foo{UInt8,Float32}

```
