# Get new type with different parameter

**URL:** https://discourse.julialang.org/t/get-new-type-with-different-parameter/37253
**Category:** New to Julia
**Created:** [April 8, 2020, 11:06pm UTC](https://discourse.julialang.org/t/get-new-type-with-different-parameter/37253 "2020-04-08T23:06:20Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)
#### Post date: [April 8, 2020, 11:06pm UTC](https://discourse.julialang.org/t/get-new-type-with-different-parameter/37253/1 "2020-04-08T23:06:20Z")

</div>

How do I get a type with a different parameter? Let’s say I have some `T where T<:SomeType{P}` and I want the type `SomeType{Q}`. I can’t just do `T{Q}` because T is a concrete type.

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [April 8, 2020, 11:19pm UTC](https://discourse.julialang.org/t/get-new-type-with-different-parameter/37253/2 "2020-04-08T23:19:01Z")

</div>

```julia
julia> struct SomeType{P} end

julia> switch(::Type{SomeType{P}}, ::Type{T}) where {P,T} = SomeType{T};

julia> switch(SomeType{Int}, Float64)
SomeType{Float64}

```

---

<div class="post-metadata">

### Author: ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)
#### Post date: [April 8, 2020, 11:27pm UTC](https://discourse.julialang.org/t/get-new-type-with-different-parameter/37253/3 "2020-04-08T23:27:42Z")

</div>

The problem is I don’t know it’s going to be `SomeType`. I just know it’s a type with one parameter and I want to change that parameter.

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [April 8, 2020, 11:35pm UTC](https://discourse.julialang.org/t/get-new-type-with-different-parameter/37253/4 "2020-04-08T23:35:55Z")

</div>

Sorry to say that this is not supported by Julia’s type system and according to those in the know, it is not a “well defined” operation.

---

<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 8, 2020, 11:36pm UTC](https://discourse.julialang.org/t/get-new-type-with-different-parameter/37253/5 "2020-04-08T23:36:51Z")

</div>

Have a read through:  
[https://docs.julialang.org/en/v1/manual/methods/#Extracting-the-type-parameter-from-a-super-type-1](https://docs.julialang.org/en/v1/manual/methods/#Extracting-the-type-parameter-from-a-super-type-1)

The thing is, that parameters might change place, or don’t exist (see the `BitVector` example). Thus, it is generally not a good idea to do this. If you still want to do it, you probably want a generated function:

```julia
julia> @generated function newT(::Type{T}, ::Type{TT}) where {T,TT}
           getfield(parentmodule(T), nameof(T)){TT}
       end
newT (generic function with 2 methods)

julia> newT(Array{Int,2}, Float64)
Array{Float64,N} where N

```

adapted from [https://github.com/JuliaObjects/ConstructionBase.jl/blob/b5686b755bd3bee29b181b3cb18fe2effa0f10a2/src/ConstructionBase.jl#L25](https://github.com/JuliaObjects/ConstructionBase.jl/blob/b5686b755bd3bee29b181b3cb18fe2effa0f10a2/src/ConstructionBase.jl#L25).

---

<div class="post-metadata">

### Author: ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)
#### Post date: [April 9, 2020, 1:26pm UTC](https://discourse.julialang.org/t/get-new-type-with-different-parameter/37253/6 "2020-04-09T13:26:10Z")

</div>

Thanks for this. Arrays are actually my use case. I wanted to implement the `SomeElType.(::MyArrayType{DifferentElType})` syntax for changing the eltype of the array that my arraytype is wrapping since that’s required to work with Optim.jl. I didn’t want to look through my `BroadcastStyle` arguments `.args` when I had all the information I needed in the types. After thinking about it a little bit more, I realize why this isn’t generally supported in the language. Since my array is always going to be the first element, it wasn’t as bad as I thought to just grab the first `.arg` and work with that directly.
