# Using parametric types for Complex{T}

**URL:** <https://discourse.julialang.org/t/using-parametric-types-for-complex-t/126489>\
**Category:** General Usage\
**Created:** [March 3, 2025, 10:10am UTC](https://discourse.julialang.org/t/using-parametric-types-for-complex-t/126489 "2025-03-03T10:10:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Miroboru](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miroboru/32/47659_2.png) [@Miroboru](https://discourse.julialang.org/u/Miroboru)\
**Post date:** [March 3, 2025, 10:10am UTC](https://discourse.julialang.org/t/using-parametric-types-for-complex-t/126489/1 "2025-03-03T10:10:12Z")

</div>

I am writing some signal processing code, and I want to be able to call it for both real and complex signals, based on either Float32 or Float64. I make no use of the real and imaginary parts of the signals (the function is a simple interpolator using Lagrange polynomials).

Right now I define my functions as

```julia
function foo(x::Vector{T}, t::T) where T <: AbstractFloat
  tmp = T(0)
  ...
end

function foo(x::Vector{Complex{T}}, t::T) where T <: AbstractFloat
  tmp = T(0)
  ...
end

```

where the complex version is identical to the real. This is not a good way to organise ones code, so I wonder if there is any way I can write this function once for all four types? I could of course not use typing in the function argument list, but then I could get a mix of doubles and floats as arguments. Any ideas or hints?

(I guess this is one example of type traits having been useful)

---

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [March 3, 2025, 10:28am UTC](https://discourse.julialang.org/t/using-parametric-types-for-complex-t/126489/2 "2025-03-03T10:28:30Z")

</div>

You can do something like:

```julia
function foo(x::Union{Vector{T}, Vector{Complex{T}}}, t::T) where T <: Union{Float32, Float64}
    tmp = T(0)
    ...
end

```

Or make it more readable with a definition:

```julia
const Signal{T} = Union{Vector{T}, Vector{Complex{T}}}

function foo(x::Signal{T}, t::T}) where T <: Union{Float32, Float64}
    tmp = T(0)
    ...
end
```

---

<div class="post-metadata">

**Author:** ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)\
**Post date:** [March 3, 2025, 10:36am UTC](https://discourse.julialang.org/t/using-parametric-types-for-complex-t/126489/3 "2025-03-03T10:36:06Z")

</div>

I would rather use

```julia
const Signal{T} = Union{Vector{T}, Vector{Complex{T}}}

function foo(x::Signal{T}, t::T}) where T
    tmp = T(0)
    ...
end

```

To avoid over-contraining : if other types can pass by you increase the value of your code.

---

<div class="post-metadata">

**Author:** ![Miroboru](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miroboru/32/47659_2.png) [@Miroboru](https://discourse.julialang.org/u/Miroboru)\
**Post date:** [March 3, 2025, 10:43am UTC](https://discourse.julialang.org/t/using-parametric-types-for-complex-t/126489/4 "2025-03-03T10:43:12Z")

</div>

Thank you - that worked out perfectly.

I really should have another look at Julia Unions, as I was under the impression that they worked much the same way as in C for primitive types only.

---

<div class="post-metadata">

**Author:** ![Miroboru](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miroboru/32/47659_2.png) [@Miroboru](https://discourse.julialang.org/u/Miroboru)\
**Post date:** [March 3, 2025, 10:47am UTC](https://discourse.julialang.org/t/using-parametric-types-for-complex-t/126489/5 "2025-03-03T10:47:48Z")

</div>

I see what you mean, but in this particular case I would probably do eg. Integer implementations slightly differently due to under- and overflows etc. One of the purposes of my code is to simulate implementations on embedded hw, so that is why I need it to be eg. pure Float32 in those cases so that I don’t miss any round-off effects somewhere…

AbstractFloat covers four types of floats which will be used identically.

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [March 3, 2025, 12:10pm UTC](https://discourse.julialang.org/t/using-parametric-types-for-complex-t/126489/6 "2025-03-03T12:10:20Z")

</div>

I would rather cast the second argument to the `eltype` of the array if the only need is to avoid mixing types:

```julia
function foo(x::Vector{T}, t_::Real) where {T<:Number}
    R = real(T)
    t = convert(R, t_)
    tmp = zero(T)
    ...
end

```

(and add specific integer implementations if needed)
