# Complex{AbstractFloat}: Not working as intended

**URL:** https://discourse.julialang.org/t/complex-abstractfloat-not-working-as-intended/82141
**Category:** New to Julia
**Tags:** question, type, parametric-types
**Created:** [June 2, 2022, 3:23pm UTC](https://discourse.julialang.org/t/complex-abstractfloat-not-working-as-intended/82141 "2022-06-02T15:23:39Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![DanDoe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dandoe/32/52717_2.png) [@DanDoe](https://discourse.julialang.org/u/DanDoe)
#### Post date: [June 2, 2022, 3:23pm UTC](https://discourse.julialang.org/t/complex-abstractfloat-not-working-as-intended/82141/1 "2022-06-02T15:23:39Z")

</div>

I want to write a type-generic function with signature  
`foo(parameter::Complex{AbstractFloat})`

such that I can use it like

```julia
Input = 42 + 42*im
foo(convert(Complex{Float32}, Input))
foo(convert(Complex{Float64}, Input))
foo(convert(Complex{BigFloat}, Input))

```

this works just fine for realtypes, i.e., without the `Complex{}`. But for complex types, I get the error:

```julia
ERROR: LoadError: MethodError: no method matching foo(::ComplexF64)
Closest candidates are:
  foo(::Complex{AbstractFloat})

```

I guess there is a shortcut for `Complex{AbstractFloat}`, but I couldn’t find it in the docs.

---

<div class="post-metadata">

### Author: ![raminammour](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raminammour/32/13572_2.png) [@raminammour](https://discourse.julialang.org/u/raminammour)
#### Post date: [June 2, 2022, 3:28pm UTC](https://discourse.julialang.org/t/complex-abstractfloat-not-working-as-intended/82141/2 "2022-06-02T15:28:58Z")

</div>

> [@DanDoe](#):
>
> foo(parameter::Complex{\<:AbstractFloat})

note the `<:`

---

<div class="post-metadata">

### Author: ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)
#### Post date: [June 2, 2022, 3:29pm UTC](https://discourse.julialang.org/t/complex-abstractfloat-not-working-as-intended/82141/3 "2022-06-02T15:29:17Z")

</div>

Use `foo(parameter::Complex{<:AbstractFloat})` or `foo(parameter::Complex{T}) where T<:AbstractFloat`  
The reason your original signature does not work is that

```julia
julia> Complex{Float64} <: Complex{AbstractFloat}
false

```

See [Types · The Julia Language](https://docs.julialang.org/en/v1/manual/types/#man-parametric-composite-types) and in particular the yellow box in that section 🙂

---

<div class="post-metadata">

### Author: ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)
#### Post date: [June 2, 2022, 5:19pm UTC](https://discourse.julialang.org/t/complex-abstractfloat-not-working-as-intended/82141/4 "2022-06-02T17:19:05Z")

</div>

Note that it’s often unnecessary to type signatures like `foo(parameter::Complex{<:AbstractFloat})`.  
Is your function really only correct for `AbstractFloat` components in the `Complex`? Most code, unless very low level, would likely work for any `Complex` number, such that `foo(parameter::Complex)` would be sufficient. Is it really not sensible to want to call `foo(3+4im)`? For that matter, `foo(6)` might still make sense and you should only require `Number`.

If the number needs to be float-backed at some point in the function, using `float(parameter)` in that position should be all you need. Although in most cases automatic [promotion](https://docs.julialang.org/en/v1/manual/conversion-and-promotion/) should make even that unnecessary.

Keeping function signatures maximally generic is what makes Julia composable and powerful. Type parameters in functions should mainly be used for dispatching to different versions of functions. You should aim to use duck typing wherever sensible.
