Tricky signature for convertible types

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

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).

(I suspect the way to go is

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)

You can do

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

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.

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