# Parametric types: complex use of multiple parameters

**URL:** https://discourse.julialang.org/t/parametric-types-complex-use-of-multiple-parameters/2152
**Category:** General Usage
**Tags:** question, parametric-types
**Created:** [February 17, 2017, 1:24pm UTC](https://discourse.julialang.org/t/parametric-types-complex-use-of-multiple-parameters/2152 "2017-02-17T13:24:47Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![mobius-eng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mobius-eng/32/99_2.png) [@mobius-eng](https://discourse.julialang.org/u/mobius-eng)
#### Post date: [February 17, 2017, 1:24pm UTC](https://discourse.julialang.org/t/parametric-types-complex-use-of-multiple-parameters/2152/1 "2017-02-17T13:24:47Z")

</div>

I have a bit of contrived example of where I have parametric type on parametric type. I have the following types expressing unbound and bound distributions:

```julia
abstract Distribution{T}

abstract UnboundDistribution{T} <: Distribution{T}
abstract BoundDistribution{T} <: Distribution{T}

```

Type parameter `T` is mainly to distinguish two main uses: some computation is performed numerically but some can be performed symbolically via `SymPy`. So, sometimes `T` is `SymPy.Sym`, and sometimes `T <: Real`.

Things get complicated when I consider truncated distribution: previously unbound (e.g. log-normal) distribution that was bound “artificially” by transforming the variable. For this I have the type:

```julia
type TruncatedDistribution{U,T <: UnboundDistribution{U}} <: AbstractTruncatedDistribution{U}
    d :: T{U}
    xmax :: U
end

```

What I want to express here is that the resulting type is going to be bound and parameterized by a type `U` (`SymPy.Sym` or `<: Real` depending on use), internally it contains an unbound distribution, parameterized by the same type `U` and maximum value of the variable of the same type `U`.

Julia (0.5) does not allow me to do this. There are two problems here:

1. I cannot refer to `U` when I specify `T`, i.e. this: `T <: UnboundDistribution{U}` is not allowed, and
2. I cannot say `T{U}` when defining `d`

If 1 were allowed, I wouldn’t need 2.

Is there a way to express all the constraints I want?

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [February 17, 2017, 1:38pm UTC](https://discourse.julialang.org/t/parametric-types-complex-use-of-multiple-parameters/2152/2 "2017-02-17T13:38:42Z")

</div>

Have you tried on Julia 0.6? This kind of thing should be allowed there.

---

<div class="post-metadata">

### Author: ![mobius-eng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mobius-eng/32/99_2.png) [@mobius-eng](https://discourse.julialang.org/u/mobius-eng)
#### Post date: [February 17, 2017, 1:51pm UTC](https://discourse.julialang.org/t/parametric-types-complex-use-of-multiple-parameters/2152/3 "2017-02-17T13:51:23Z")

</div>

I am going to try now. Although, I am a bit cautious of jumping to a newer version just yet as some packages might break (`SymPy` already causes deprecation warnings, but that I think is because of `PyCall`).

So, what is allowed in Julia 0.6? I can refer to `U` when I am constraining `T` or I can put `T{U}`?

---

<div class="post-metadata">

### Author: ![Steven\_Sagaert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steven_sagaert/32/29578_2.png) [@Steven\_Sagaert](https://discourse.julialang.org/u/Steven_Sagaert)
#### Post date: [February 17, 2017, 2:00pm UTC](https://discourse.julialang.org/t/parametric-types-complex-use-of-multiple-parameters/2152/4 "2017-02-17T14:00:28Z")

</div>

This related discussion (although the question is about a more general case) might be useful:

> [@Are there any (long term) plans to add higher order generics (a.k.a. higher kinded types) to Julia?](https://discourse.julialang.org/t/are-there-any-long-term-plans-to-add-higher-order-generics-a-k-a-higher-kinded-types-to-julia/1570):
>
> Higher order generics are useful to write very generic libraries over container data structures. In Julia such a type would look like C1{C2{T}} with C1 & C2 container types (Arrays,Lists,…) for a second order type. There could be even more abstract n-order types. If we ignore very theoretical languages like Agda,Idris,Coq, the only languages that have this - that I know of - are Haskell & Scala. Both are very powerful languages. I think Julia could benefit from such a feature as well. I wonde…

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [February 17, 2017, 2:04pm UTC](https://discourse.julialang.org/t/parametric-types-complex-use-of-multiple-parameters/2152/5 "2017-02-17T14:04:40Z")

</div>

> [@mobius-eng](#):
>
> type TruncatedDistribution{U,T \<: UnboundDistribution{U}}

I believe you can do `type TruncatedDistribution{U,T <: UnboundDistribution{U}}`

Note that the keyword `type` also changes to `struct`, or `mutable struct` if it needs to be mutable.

---

<div class="post-metadata">

### Author: ![mobius-eng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mobius-eng/32/99_2.png) [@mobius-eng](https://discourse.julialang.org/u/mobius-eng)
#### Post date: [February 17, 2017, 3:51pm UTC](https://discourse.julialang.org/t/parametric-types-complex-use-of-multiple-parameters/2152/6 "2017-02-17T15:51:15Z")

</div>

Thanks, this work indeed!

---

<div class="post-metadata">

### Author: ![mobius-eng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mobius-eng/32/99_2.png) [@mobius-eng](https://discourse.julialang.org/u/mobius-eng)
#### Post date: [February 18, 2017, 6:00pm UTC](https://discourse.julialang.org/t/parametric-types-complex-use-of-multiple-parameters/2152/7 "2017-02-18T18:00:14Z")

</div>

A bit out of luck, as somewhat expected, Plots.jl don’t work yet with Julia 0.6… 😞
