# Stripping parameter from parametric types

**URL:** https://discourse.julialang.org/t/stripping-parameter-from-parametric-types/8293
**Category:** New to Julia
**Tags:** parametric-types
**Created:** [January 11, 2018, 3:03pm UTC](https://discourse.julialang.org/t/stripping-parameter-from-parametric-types/8293 "2018-01-11T15:03:46Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [January 11, 2018, 4:52pm UTC](https://discourse.julialang.org/t/stripping-parameter-from-parametric-types/8293/2 "2018-01-11T16:52:40Z")

</div>

Not sure if there is an easier way or not, but here is my attempt.

```julia
julia> name(T::DataType) = T.name
name (generic function with 1 method)

julia> name(T::UnionAll) = name(T.body)
name (generic function with 2 methods)

julia> datatype(T::UnionAll) = datatype(T.body)
datatype (generic function with 1 method)

julia> datatype(T::DataType) = T
datatype (generic function with 2 methods)

julia> nvars(T::UnionAll) = 1 + nvars(T.body)
nvars (generic function with 1 method)

julia> nvars(T::DataType) = 0
nvars (generic function with 2 methods)

julia> unionall(T) = eval(Symbol(name(T)))
unionall (generic function with 1 method)

julia> parametersless1(T) = datatype(T).parameters[1:end-nvars(T)-1]
parametersless1 (generic function with 1 method)

julia> @generated function f(::Type{T}) where {T}
           any(isa.(T, (DataType, UnionAll))) || throw("Only supports DataType and UnionAll inputs.")
           n = unionall(T)
           n isa DataType && return :($n)
           ps = parametersless1(T)
           return :($n{$(ps...)})
       end
f (generic function with 1 method)

julia> struct T{S1,S2,S3} end

julia> f(T{1,2,3})
T{1,2,S3} where S3

julia> f(T{1,2})
T{1,S2,S3} where S3 where S2

julia> f(T{1})
T

julia> f(T)
T

julia> f(Int)
Int64

```

You can also make a normal function not a generated one. And you can avoid using `eval` altogether if you find a way to change a `TypeName` to a `UnionAll` in case of parametric types, or to a `DataType` in case of non-parametric types, `eval` was the lazy solution.

---

_[View the full topic](https://discourse.julialang.org/t/stripping-parameter-from-parametric-types/8293)._
