# Stripping type parameters

**URL:** <https://discourse.julialang.org/t/stripping-type-parameters/10462>\
**Category:** General Usage\
**Created:** [April 20, 2018, 8:23pm UTC](https://discourse.julialang.org/t/stripping-type-parameters/10462 "2018-04-20T20:23:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![atteson](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@atteson](https://discourse.julialang.org/u/atteson)\
**Post date:** [April 20, 2018, 8:23pm UTC](https://discourse.julialang.org/t/stripping-type-parameters/10462/1 "2018-04-20T20:23:38Z")

</div>

Is there a way to strip type parameters from a type. I have in mind something like this:

```julia
f(x::T{U}) where T where U = T

```

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [April 20, 2018, 11:27pm UTC](https://discourse.julialang.org/t/stripping-type-parameters/10462/2 "2018-04-20T23:27:48Z")

</div>

Usually you don’t need to. Do you have an example to illustrate a possible need for it?

---

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [April 21, 2018, 12:21am UTC](https://discourse.julialang.org/t/stripping-type-parameters/10462/3 "2018-04-21T00:21:47Z")

</div>

Here’s a hack.

```julia
julia> f(x) = eval(x.name.name)
julia> f(Complex{Int64})
Complex

```

I have no idea why this works, but it does.

---

<div class="post-metadata">

**Author:** ![atteson](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@atteson](https://discourse.julialang.org/u/atteson)\
**Post date:** [April 21, 2018, 2:35pm UTC](https://discourse.julialang.org/t/stripping-type-parameters/10462/4 "2018-04-21T14:35:20Z")

</div>

My use case is a bit complicated. I’m trying to build some code to generically find the maximum likelihood estimate for a distribution on a sample. The optimizer needs derivatives so I’m trying to use ReverseDiff. ReverseDiff doesn’t work on distributions which have been instantiated with specific types. For example, the following demonstrates simplified code that doesn’t work and the subsequent code (using the stripped Normal distribution) which does:

```julia
using ReverseDiff
using Distributions

n = Normal()
sample = rand(n,1000);
f(x) = loglikelihood( typeof(n)(x...), sample )
ReverseDiff.gradient( f, [params(n)...] ) # doesn't work; returns zeros

g(x) = loglikelihood( Normal(x...), sample )
ReverseDiff.gradient( g, [params(n)...] ) # works

```

---

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [April 21, 2018, 4:58pm UTC](https://discourse.julialang.org/t/stripping-type-parameters/10462/5 "2018-04-21T16:58:13Z")

</div>

> [@atteson](#):
>
> f(x) = loglikelihood( typeof(n)(x…), sample )

Try

```julia
f(x) = loglikelihood( typeof(n).instance(x...), sample )

```

---

<div class="post-metadata">

**Author:** ![atteson](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@atteson](https://discourse.julialang.org/u/atteson)\
**Post date:** [April 21, 2018, 5:23pm UTC](https://discourse.julialang.org/t/stripping-type-parameters/10462/6 "2018-04-21T17:23:27Z")

</div>

That didn’t work for me. I’m on v0.6:

```julia
julia> h(x) = loglikelihood( typeof(n).instance(x...), sample )
h (generic function with 1 method)

julia> h([params(n)...])
ERROR: UndefRefError: access to undefined reference
Stacktrace:
 [1] h(::Array{Float64,1}) at ./none:1

julia> 

```

---

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [April 21, 2018, 5:44pm UTC](https://discourse.julialang.org/t/stripping-type-parameters/10462/7 "2018-04-21T17:44:45Z")

</div>

Sorry about that. `typeof(Normal())` is `Distributions.Normal{Float64}’ which is not a function.

---

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [April 21, 2018, 8:43pm UTC](https://discourse.julialang.org/t/stripping-type-parameters/10462/8 "2018-04-21T20:43:58Z")

</div>

I finally got this:

```julia
using ReverseDiff
using Distributions

n = Normal()
sample = rand(n,1000);
f(x) = loglikelihood( (typeof(n).name.wrapper)(x...), sample )

julia> ReverseDiff.gradient( f, [params(n)...] )
2-element Array{Float64,1}:
  2.29938
 35.7245 

```

Is that what you expected?
