Parametric types: complex use of multiple parameters

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:

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:

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

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?

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

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}?

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

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.

Thanks, this work indeed!

1 Like

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