# Inspect parameter type in container-like class

**URL:** <https://discourse.julialang.org/t/inspect-parameter-type-in-container-like-class/1492>\
**Category:** General Usage\
**Tags:** faq\
**Created:** [January 15, 2017, 5:38am UTC](https://discourse.julialang.org/t/inspect-parameter-type-in-container-like-class/1492 "2017-01-15T05:38:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [January 15, 2017, 5:38am UTC](https://discourse.julialang.org/t/inspect-parameter-type-in-container-like-class/1492/1 "2017-01-15T05:38:33Z")

</div>

I have a very common problem for which I still don’t have a clear solution in Julia:

```julia
abstract AbstractFoo{N,T}
dimension{N,T}(::AbstractFoo{N,T}) = N
coordtype{N,T}(::AbstractFoo{N,T}) = T

abstract AbstractContainer{F}

immutable Container{F<:AbstractFoo} <: AbstractContainer{F}
  state::Vector{coordtype(F)} # ERROR: MethodError: no method matching coordtype(::TypeVar)
end

```

The natural design above in which the container is parametrized by its element type doesn’t work. This problem was initially introduced in [another thread](https://discourse.julialang.org/t/recovering-parameter-type-from-parameter-list/1343) but I think it deserves a thread on its own as well as a step-by-step explanation on what is the best current solution in Julia v0.5.

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)\
**Post date:** [January 15, 2017, 5:57am UTC](https://discourse.julialang.org/t/inspect-parameter-type-in-container-like-class/1492/2 "2017-01-15T05:57:43Z")

</div>

Issue [#18466](https://github.com/JuliaLang/julia/issues/18466) linked in the other thread answers the question: this is not currently possible. Several workable suggestions were given there.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [January 15, 2017, 6:02am UTC](https://discourse.julialang.org/t/inspect-parameter-type-in-container-like-class/1492/3 "2017-01-15T06:02:28Z")

</div>

Thank you @ihnorton, I am looking for the best workable suggestion, it is still not clear to me.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 15, 2017, 7:34am UTC](https://discourse.julialang.org/t/inspect-parameter-type-in-container-like-class/1492/4 "2017-01-15T07:34:51Z")

</div>

The current solution seems to be roughly this:

1. for each transformation/constraint calculated in type space, introduce a new type parameter
2. check that in the inner constructor

Something along the lines of

```julia
abstract AbstractFoo{N,T}
dimension{N,T}(::AbstractFoo{N,T}) = N
coordtype{N,T}(::AbstractFoo{N,T}) = T

abstract AbstractContainer{F}

immutable Container{F<:AbstractFoo,S} <: AbstractContainer{F}
    state::Vector{S}
    function Container(state)
        @assert S == coordtype(F)
        new(state)
    end
end

```

if I understood your problem correctly.

I used to think that this was tedious, but I now think that it is the best way to do it, because there are some constraints you can’t express in a triangular manner anyway.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [January 15, 2017, 4:15pm UTC](https://discourse.julialang.org/t/inspect-parameter-type-in-container-like-class/1492/5 "2017-01-15T16:15:31Z")

</div>

Thank you @Tamas_Papp, what do you mean by transformation/constraint?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 15, 2017, 4:23pm UTC](https://discourse.julialang.org/t/inspect-parameter-type-in-container-like-class/1492/6 "2017-01-15T16:23:33Z")

</div>

`coordtype` above is a transformation (function): it maps `F` to `S`.

More generally, one could enforce that some `g(S,F)` holds, even when that is not amenable to a `S=h(F)` mapping.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [January 15, 2017, 4:27pm UTC](https://discourse.julialang.org/t/inspect-parameter-type-in-container-like-class/1492/7 "2017-01-15T16:27:23Z")

</div>

Do you have an example in which a more general relation `g(S,F)` is needed?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 15, 2017, 4:51pm UTC](https://discourse.julialang.org/t/inspect-parameter-type-in-container-like-class/1492/8 "2017-01-15T16:51:58Z")

</div>

Suppose that for some calculation with floats, I want `F` to be at least as precise as `S` (in the sense of `Float16 < Float32 < Float64 < ...`), but I don’t want to determine how precise (it’s up to the user), other than “at least `Float32`”. So I could do

```julia
function g(S,F)
    if S <: AbstractFloat && F <: AbstractFloat
        _rank(f) = findfirst([Float16, Float32, Float64, BigFloat], f)
        max(_rank(S),2) ≤ _rank(F)
    else
        false
    end
end

```

and enforce this with the constructor.
