# Parametric type with parametric type field

**URL:** https://discourse.julialang.org/t/parametric-type-with-parametric-type-field/1321
**Category:** General Usage
**Tags:** parametric-types
**Created:** [January 6, 2017, 11:02am UTC](https://discourse.julialang.org/t/parametric-type-with-parametric-type-field/1321 "2017-01-06T11:02:58Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![pearcemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pearcemc/32/3109_2.png) [@pearcemc](https://discourse.julialang.org/u/pearcemc)
#### Post date: [January 6, 2017, 11:02am UTC](https://discourse.julialang.org/t/parametric-type-with-parametric-type-field/1321/1 "2017-01-06T11:02:58Z")

</div>

Suppose I want to define a type like:

```julia
abstract AbstractSubThing{T}

type SubThing{T} <: AbstractSubThing
  item::T
end

# the trouble
type WrapThing{Tx, Ty<:AbstractSubThing{Tx}}
    sub::Ty
    outer::Tx
end
#ERROR: UndefVarError: Tx not defined

type WrapThing{Tx, Ty<:AbstractSubThing}
  sub::Ty{Tx}
  outer::Tx
end
#ERROR: TypeError: Type{...} expression: expected Type{T}, got TypeVar

```

Essentially I want to have `WrapThing` enforce type consistency so it only accepts `SubThing`s which are consistent with its other arguments. E.g.

```julia
WrapThing(SubThing(1.0), 1.0) #good
WrapThing(SubThing(1.0), 1) #should throw error

```

I’ve tried a bunch of different plausible seeming strategies. Clearly I’m missing some of the finer points of the type system, but don’t know where I’m going wrong.

While needing to be able to swap out `SubThing` for some other flavour of `AbstractSubThing` later (e.g. in my problem a different database backend).

---

<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 6, 2017, 12:01pm UTC](https://discourse.julialang.org/t/parametric-type-with-parametric-type-field/1321/2 "2017-01-06T12:01:13Z")

</div>

AFAIK this cannot be done with the current type system. You can use an inner constructor to inspect and enforce type constraints. See  
[https://github.com/JuliaLang/julia/issues/8974](https://github.com/JuliaLang/julia/issues/8974)  
[https://github.com/JuliaLang/julia/pull/18457](https://github.com/JuliaLang/julia/pull/18457)

---

<div class="post-metadata">

### Author: ![pearcemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pearcemc/32/3109_2.png) [@pearcemc](https://discourse.julialang.org/u/pearcemc)
#### Post date: [January 9, 2017, 2:31pm UTC](https://discourse.julialang.org/t/parametric-type-with-parametric-type-field/1321/4 "2017-01-09T14:31:54Z")

</div>

Tamas, thanks, I’ll take a break from failing to find a workaround 🙂
