# Tricky signature for convertible types

**URL:** <https://discourse.julialang.org/t/tricky-signature-for-convertible-types/32145>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 11, 2019, 11:40am UTC](https://discourse.julialang.org/t/tricky-signature-for-convertible-types/32145 "2019-12-11T11:40:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Post date:** [December 11, 2019, 11:40am UTC](https://discourse.julialang.org/t/tricky-signature-for-convertible-types/32145/1 "2019-12-11T11:40:51Z")

</div>

Hi, I’m trying to figure out whether its possible to write a signature for a function `f(x, y)` such that `y` can be converted to `x` via `convert`. Actually it is a bit simpler: I want to constraint `x` and `y` to be both _either_ `<:SMatrix{N,N}` with the same `N` or both `<:Number`.

I could of course write

```julia
f(x::Number, y::Number) = ...
f(x::SMatrix{N,N}, y::SMatrix{N,N}) where {N} = ...

```

but then I would need to write the same code twice (both methods are identical). In order to follow DRY I would like to write a single signature that picks both cases. Is it possible? (Note that making both `x` and `y` `::Union{Number, SMatrix{N,N}}` doesn’t work, as it doesn’t exclude the case of having one `Number` and one `SMatrix`).

---

<div class="post-metadata">

**Author:** ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Post date:** [December 11, 2019, 11:52am UTC](https://discourse.julialang.org/t/tricky-signature-for-convertible-types/32145/2 "2019-12-11T11:52:28Z")

</div>

(I suspect the way to go is

```julia
f(x::Number, y::Number) = _f(x, y)
f(x::SMatrix{N,N}, y::SMatrix{N,N}) where {N} = _f(x, y)
_f(x, y) = ...

```

but just wanted to make sure)

---

<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:** [December 11, 2019, 11:56am UTC](https://discourse.julialang.org/t/tricky-signature-for-convertible-types/32145/3 "2019-12-11T11:56:26Z")

</div>

You can do

```julia
f(x::T, y::T) where {T <: Union{Number, SMatrix{N,N} where N}} = ...

```

but I find the version with `_f` much cleaner, TBH.

If you are doing this a lot, consider defining a type alias for that whole mess

```julia
const _AcceptedTypes = Union{Number, SMatrix{N,N} where N}

```

of course only for internal use.

**EDIT** : my version precludes eg `f(1, 1.0)`, not sure it you need that, or if you want to just promote.

---

<div class="post-metadata">

**Author:** ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Post date:** [December 11, 2019, 12:00pm UTC](https://discourse.julialang.org/t/tricky-signature-for-convertible-types/32145/4 "2019-12-11T12:00:01Z")

</div>

Hey Tamas, thanks for your reply. Yes, I do need to accept `f(1, 1.0)` (different but promotable types) so I guess I’ll have to go with `_f`
