# Multiple dispatch on StructArrays

**URL:** <https://discourse.julialang.org/t/multiple-dispatch-on-structarrays/43810>\
**Category:** General Usage\
**Tags:** question, multidispatch, structarrays\
**Created:** [July 28, 2020, 10:11am UTC](https://discourse.julialang.org/t/multiple-dispatch-on-structarrays/43810 "2020-07-28T10:11:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ozmaden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ozmaden/32/14438_2.png) [@ozmaden](https://discourse.julialang.org/u/ozmaden)\
**Post date:** [July 28, 2020, 10:11am UTC](https://discourse.julialang.org/t/multiple-dispatch-on-structarrays/43810/1 "2020-07-28T10:11:29Z")

</div>

Hi everyone. I am working with the `StructArrays.jl` package and trying to program a function to react differently for different types of arrays inside of a StructArray.

Specifically I want to differentiate between Array and CuArray of ComplexF32 values. I have found the most straighforward (and honestly ugly looking) way to do this.

```julia
# Creating the arrays
cpuarray = Array{ComplexF32}(undef, 2)
gpuarray = CuArray(cpuarray)
scpuarray = StructArray{ComplexF32}((real(cpuarray),imag(cpuarray)))
sgpuarray = StructArray{ComplexF32}((real(gpuarray),imag(gpuarray)))

# Checking types
julia> typeof(scpuarray)
StructArray{Complex{Float32},1,NamedTuple{(:re, :im),Tuple{Array{Float32,1},Array{Float32,1}}},Int64}
julia> typeof(sgpuarray)
StructArray{Complex{Float32},1,NamedTuple{(:re, :im),Tuple{CuArray{Float32,1,Nothing},CuArray{Float32,1,Nothing}}},Int64}

# Defining a basic function for both scpuarray and sgpuarray 
function example(
    input::StructArray{Complex{Float32},1,NamedTuple{(:re, :im),Tuple{Array{Float32,1},Array{Float32,1}}},Int64}
)
    return 1
end
function example(
    input::StructArray{Complex{Float32},1,NamedTuple{(:re, :im),Tuple{CuArray{Float32,1,Nothing},CuArray{Float32,1,Nothing}}},Int64}
)
    return 2
end

# Checking the definitions
julia> example(scpuarray)
1
julia> example(sgpuarray)
2

```

Are there any other ideas on how achieve this in a more elegant way?  
Ideally I would just like to write something like `input::StructArray(CuArray)`  
Would appreciate any help.

---

<div class="post-metadata">

**Author:** ![Iulian.Cioarca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iulian.cioarca/32/30166_2.png) [@Iulian.Cioarca](https://discourse.julialang.org/u/Iulian.Cioarca)\
**Post date:** [July 28, 2020, 10:31am UTC](https://discourse.julialang.org/t/multiple-dispatch-on-structarrays/43810/2 "2020-07-28T10:31:42Z")

</div>

Are you concerned only with the length of the type names? You could write some type aliases if there aren’t too many of them.

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [July 28, 2020, 11:25am UTC](https://discourse.julialang.org/t/multiple-dispatch-on-structarrays/43810/3 "2020-07-28T11:25:43Z")

</div>

Dispatch on a Trait?

---

<div class="post-metadata">

**Author:** ![ozmaden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ozmaden/32/14438_2.png) [@ozmaden](https://discourse.julialang.org/u/ozmaden)\
**Post date:** [July 28, 2020, 2:52pm UTC](https://discourse.julialang.org/t/multiple-dispatch-on-structarrays/43810/4 "2020-07-28T14:52:34Z")

</div>

> [@Iulian.Cioarca](#):
>
> Are you concerned only with the length of the type names?

Sort of. I feel like the type name could have been more “readable” if you wish. I guess that’s my issue with it. Just like I mentioned before, it would have been nice to see something along the lines of:

```julia
# Pseudocode
function example(
       input::StructArray{ComplexF32}(Array, Array)
)
       return 1
end

```

> [@Iulian.Cioarca](#):
>
> You could write some type aliases if there aren’t too many of them.

You mean like this?

```julia
function example(
           input::StructArray{Complex{Float32},1,A,Int64}
       ) where {
       A <: NamedTuple{(:re, :im),Tuple{Array{Float32,1},Array{Float32,1}}}
       }
           return 1
       end
julia> function example(
           input::StructArray{Complex{Float32},1,A,Int64}
       ) where {
       A <: NamedTuple{(:re, :im),Tuple{Array{Float32,1},Array{Float32,1}}}
       }
           return 1
       end

```

---

<div class="post-metadata">

**Author:** ![ozmaden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ozmaden/32/14438_2.png) [@ozmaden](https://discourse.julialang.org/u/ozmaden)\
**Post date:** [July 28, 2020, 2:56pm UTC](https://discourse.julialang.org/t/multiple-dispatch-on-structarrays/43810/5 "2020-07-28T14:56:14Z")

</div>

Not really familiar with this concept. Is this what you’re mentioning? [https://github.com/mauro3/SimpleTraits.jl](https://github.com/mauro3/SimpleTraits.jl)  
Would be very kind of you to link some resources, I will look into it.

---

<div class="post-metadata">

**Author:** ![Iulian.Cioarca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iulian.cioarca/32/30166_2.png) [@Iulian.Cioarca](https://discourse.julialang.org/u/Iulian.Cioarca)\
**Post date:** [July 28, 2020, 3:14pm UTC](https://discourse.julialang.org/t/multiple-dispatch-on-structarrays/43810/6 "2020-07-28T15:14:47Z")

</div>

Something like that. Or you could define the alias somewhere else in the code:

```julia
B = NamedTuple{(:re, :im),Tuple{Array{Float32,1},Array{Float32,1}}}
function example2(input::StructArray{Complex{Float32},1,B,Int64}) where B
    return 1
end

```

or define a type alias for the whole thing:

```julia
StructCpuArray = StructArray{Complex{Float32},1,NamedTuple{(:re, :im),Tuple{Array{Float32,1},Array{Float32,1}}},Int64}
function example3(input::StructCpuArray)
    return 1
end

```

---

<div class="post-metadata">

**Author:** ![ozmaden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ozmaden/32/14438_2.png) [@ozmaden](https://discourse.julialang.org/u/ozmaden)\
**Post date:** [July 28, 2020, 3:21pm UTC](https://discourse.julialang.org/t/multiple-dispatch-on-structarrays/43810/7 "2020-07-28T15:21:33Z")

</div>

Thanks! The second option is probably the most obvious and pretty one.

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [July 28, 2020, 7:15pm UTC](https://discourse.julialang.org/t/multiple-dispatch-on-structarrays/43810/8 "2020-07-28T19:15:21Z")

</div>

Yes, SimpleTraits.jl is a good package for traits. [BinaryTraits.jl](https://github.com/tk3369/BinaryTraits.jl) is also very easy to use.

The drawback of the type alias is that you essential dispatch on implementation details of StructArrays.jl (the NamedTuple etc). That looks brittle to me.
