# Why can't i write such a function?

**URL:** <https://discourse.julialang.org/t/why-cant-i-write-such-a-function/11953>\
**Category:** General Usage\
**Created:** [June 26, 2018, 2:07am UTC](https://discourse.julialang.org/t/why-cant-i-write-such-a-function/11953 "2018-06-26T02:07:51Z")\
**Posts on this page:** 7\
**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:** [June 26, 2018, 2:07am UTC](https://discourse.julialang.org/t/why-cant-i-write-such-a-function/11953/1 "2018-06-26T02:07:51Z")

</div>

```julia
function get_typparam(::Type{X{Q1,Q2,Q3}}) where X where {Q1,Q2,Q3}
           [Q1,Q2,Q3]
 end

```

ERROR: TypeError: Type{…} expression: expected UnionAll, got TypeVar

Thanks

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [June 26, 2018, 3:10am UTC](https://discourse.julialang.org/t/why-cant-i-write-such-a-function/11953/2 "2018-06-26T03:10:59Z")

</div>

is `X` a type? You might be confusing the syntax `x <: Int` from `x <: Type{Int}`

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 26, 2018, 3:35am UTC](https://discourse.julialang.org/t/why-cant-i-write-such-a-function/11953/3 "2018-06-26T03:35:17Z")

</div>

Allowing that could make dispatch undecidable.

---

<div class="post-metadata">

**Author:** ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)\
**Post date:** [June 26, 2018, 4:03am UTC](https://discourse.julialang.org/t/why-cant-i-write-such-a-function/11953/4 "2018-06-26T04:03:15Z")

</div>

Not sure if that’s useful but I was trying to do similar gymnastics, asked [a question](https://stackoverflow.com/questions/42902872/recover-type-in-parametric-composite-type) on stackoverflow where it was kindly explained to me that something like this is the neat way of recovering the type of a struct:

```julia
struct Foo{T<:Real}
  x::T 
end
bar(f::Foo{T}) where T = T  
bar(Foo(2.0)) # --> Float64
bar(Foo(2)) # --> Int

```

It can probably be done better but I thought it helped clarify types & multiple dispatch. (Apologies if that’s not at all what you’re trying to do)

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [June 26, 2018, 8:01am UTC](https://discourse.julialang.org/t/why-cant-i-write-such-a-function/11953/5 "2018-06-26T08:01:49Z")

</div>

I think `X` as a fully unrestricted type is not possible,  
If `X` is the subtype of some abstract type that has the type parameters you are interested in you can

You can do something like  
[https://github.com/JuliaText/Embeddings.jl/blob/65b658865e476981c4c74988018274e77dcd0b03/src/Embeddings.jl#L32-L36](https://github.com/JuliaText/Embeddings.jl/blob/65b658865e476981c4c74988018274e77dcd0b03/src/Embeddings.jl#L32-L36)

So you can do, if `X` is always a subtype of `AbstractFoo{Q1,Q2,Q3}`

```julia
function get_typparam(::Type{X}) where {X <: AbstractFoo{Q1,Q2,Q3}} where {Q1,Q2,Q3}
           [Q1,Q2,Q3]
end

```

I think.  
(not tested)

---

<div class="post-metadata">

**Author:** ![m-j-w](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/m-j-w/32/4541_2.png) [@m-j-w](https://discourse.julialang.org/u/m-j-w)\
**Post date:** [June 26, 2018, 3:12pm UTC](https://discourse.julialang.org/t/why-cant-i-write-such-a-function/11953/6 "2018-06-26T15:12:27Z")

</div>

You also get the type parameters of a type by inspecting the type variable itself:

```julia
function type_params(x::Type)
    t = Base.unwrap_unionall(x) # unpack a unionall in case not all type parameters are specified
    return (t.parameters...,) # convert to a regular tuple
end

# test case
struct X{A,B,C} end

x1 = X{Int}
x2 = x1{Real}
x3 = x2{3}

```

This prints

```julia
Main> type_params(x1)
(Int64, B, C)

Main> type_params(x2)
(Int64, Real, C)

Main> type_params(x3)
(Int64, Real, 3)

```

---

<div class="post-metadata">

**Author:** ![louisponet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/louisponet/32/2070_2.png) [@louisponet](https://discourse.julialang.org/u/louisponet)\
**Post date:** [June 26, 2018, 7:33pm UTC](https://discourse.julialang.org/t/why-cant-i-write-such-a-function/11953/7 "2018-06-26T19:33:09Z")

</div>

I did something like this once, that worked as well, not sure if this is what you’re trying to do though 😃

```julia
eltypes(::Type{Cache{T, Dim, CC, CF}}) where {T, Dim, CC, CF} = (T, Dim,CC, CF)
eltypes(p) = eltypes(typeof(p))

```
