# UnionAll as a Type Parameter

**URL:** <https://discourse.julialang.org/t/unionall-as-a-type-parameter/75585>\
**Category:** General Usage\
**Tags:** parametric-types\
**Created:** [February 1, 2022, 4:23pm UTC](https://discourse.julialang.org/t/unionall-as-a-type-parameter/75585 "2022-02-01T16:23:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RGon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rgon/32/19515_2.png) [@RGon](https://discourse.julialang.org/u/RGon)\
**Post date:** [February 1, 2022, 4:23pm UTC](https://discourse.julialang.org/t/unionall-as-a-type-parameter/75585/1 "2022-02-01T16:23:26Z")

</div>

I’m trying to build a structure that separates parametric types (UnionAll) into three categories (Gaussian Alarms, Gaussian Observations, and Reset Observations). I regularly deal with many different UnionAll types. I tried to do something like

```julia
struct ObservationStruct{T}
    alarms :: T{GaussianAlarm}
    estimators :: T{GaussianMeas}
    resets :: T{ResetObs}
end

```

but I get an error  
`LoadError: TypeError: in Type{...} expression, expected UnionAll, got a value of type TypeVar`  
Is there any trick here that would allow me to use a UnionAll type for T? Similarly, I want to try and extract the UnionAll type one level up

```julia
basetype(::Type{U{T}}) where {U<:UnionAll, T} = U

```

and I get a very similar error  
`TypeError: in Type{...} expression, expected UnionAll, got a value of type TypeVar`

Are there any tricks with respect to handling nested parametric types? For example, an generic way to get  
`Dict{Symbol, Vector{T}} where T` from `Dict{Symbol, Vector{Float64}}`  
but at the same time get  
`Vector{Dict{Symbol, T}} where T` from `Vector{Dict{Symbol, Float64}}`

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [February 1, 2022, 4:59pm UTC](https://discourse.julialang.org/t/unionall-as-a-type-parameter/75585/2 "2022-02-01T16:59:11Z")

</div>

I’m not sure I understand what you are trying to achieve. Is it something like

```julia
abstract type AbstractParametricType{T} end

struct GaussianAlarm end
struct GaussianMeas end
struct ResetObs end

struct ObservationStruct{
    GA <: AbstractParametricType{GaussianAlarm}, 
    GM <: AbstractParametricType{GaussianMeas}, 
    RO <: AbstractParametricType{ResetObs}}
    alarms ::GA 
    estimators ::GM 
    resets ::RO
end

struct ParametricType1{T} <: AbstractParametricType{T} end
ObservationStruct(
    ParametricType1{GaussianAlarm}(), 
    ParametricType1{GaussianMeas}(),
    ParametricType1{ResetObs}())
struct ParametricType2{T} <: AbstractParametricType{T} end
ObservationStruct(
    ParametricType2{GaussianAlarm}(), 
    ParametricType2{GaussianMeas}(),
    ParametricType2{ResetObs}())

```

?

---

<div class="post-metadata">

**Author:** ![RGon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rgon/32/19515_2.png) [@RGon](https://discourse.julialang.org/u/RGon)\
**Post date:** [February 1, 2022, 6:48pm UTC](https://discourse.julialang.org/t/unionall-as-a-type-parameter/75585/3 "2022-02-01T18:48:50Z")

</div>

I wanted to be able to have something like

```julia
ObservationStruct{ Dict{Symbol, Vector{T}} where T}()
>> ObservationStruct{ Dict{Symbol, Vector{T}} where T}(
>> alarms = Dict{Symbol, Vector{GaussianAlarm}}()
>> estimators = Dict{Symbol, Vector{GaussianEstimator}}()
>> resets = Dict{Symbol, Vector{ResetObs}() )

```

I could simply have `ObservationStruct{T1, T2, T3}` and then write a constructor that would use the UnionAll type, but then it would be hard for me to extract the original UnionAll  
`Dict{Symbol, Vector{T}} where T`  
that I used to construct the ObservationStruct. I also would have a hard time dispatching based on the different UnionAll values I used to construct the ObservationStruct. This is why I try hard to avoid deeply nested structs in Julia, but this time there’s no way around it and I’m trying to figure out how to do it elegantly.

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [February 1, 2022, 9:56pm UTC](https://discourse.julialang.org/t/unionall-as-a-type-parameter/75585/4 "2022-02-01T21:56:58Z")

</div>

> [@RGon](#):
>
> ```julia
> ObservationStruct{ Dict{Symbol, Vector{T}} where T}()
> >> ObservationStruct{ Dict{Symbol, Vector{T}} where T}(
> >> alarms = Dict{Symbol, Vector{GaussianAlarm}}()
> >> estimators = Dict{Symbol, Vector{GaussianEstimator}}()
> >> resets = Dict{Symbol, Vector{ResetObs}() )
> 
> ```

With

```julia
struct ParametricType1{T} <: AbstractParametricType{T} 
    dict::Dict{Symbol, Vector{T}}
    ParametricType1{T}() where T = new{T}(Dict{Symbol, Vector{T}}())
end

```

we should be pretty near (modulo one indirection)?
