Get new type with different parameter

Have a read through:
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> @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.

3 Likes