# How to define a parametric struct variable which could be an array or a scalar?

**URL:** <https://discourse.julialang.org/t/how-to-define-a-parametric-struct-variable-which-could-be-an-array-or-a-scalar/21301>\
**Category:** General Usage\
**Created:** [February 28, 2019, 1:23pm UTC](https://discourse.julialang.org/t/how-to-define-a-parametric-struct-variable-which-could-be-an-array-or-a-scalar/21301 "2019-02-28T13:23:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![zsoerenm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zsoerenm/32/664_2.png) [@zsoerenm](https://discourse.julialang.org/u/zsoerenm)\
**Post date:** [February 28, 2019, 1:23pm UTC](https://discourse.julialang.org/t/how-to-define-a-parametric-struct-variable-which-could-be-an-array-or-a-scalar/21301/1 "2019-02-28T13:23:55Z")

</div>

Depending on the inputs `x, P, F` of a function `test` the output can be of type array or scalar:

```julia
struct Results{A <: Union{Float64, <: AbstractVector{Float64}}, B <: Union{Float64, <: AbstractMatrix{Float64}}}
    a::A
    b::B
end  
function test(x, P, F)
    Results(F * x, F * P * F')
end

```

```julia-repl
julia> test([1., 0.], [1. 0.; 0. 1.], [1. 0.; 0. 1.])
Results{Array{Float64,1},Array{Float64,2}}([1.0, 0.0], [1.0 0.0; 0.0 1.0])
julia> test([1., 0.], [1. 0.; 0. 1.], [1., 0.]')
Results{Float64,Float64}(1.0, 1.0)

```

This works. However, I’d like Float64 to be parametric. Simply replacing Float64 with T does not work:

```julia
struct Results{A <: Union{T, <: AbstractVector{T}}, B <: Union{T, <: AbstractMatrix{T}}}
    a::A
    b::B
end

```

This results in: `ERROR: UndefVarError: T not defined`

---

<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:** [February 28, 2019, 1:31pm UTC](https://discourse.julialang.org/t/how-to-define-a-parametric-struct-variable-which-could-be-an-array-or-a-scalar/21301/2 "2019-02-28T13:31:45Z")

</div>

You would make `T` the first type parameter.

That said, I would not worry about it, and just define

```julia
struct Results{A,B}
    a::A
    b::B
end

```

and dispatch on them accordingly.
