How to enforce minimum dimensions for arrays that might be scalars?

If the operations ... will work equally well for numbers, then you can define the function as e.g. f(m::AbstractMatrix, v::Union{Number, AbstractVector}) or even just f(m, v). A different version will still be compiled for each combination of concrete types.

If not, and you need a separate implementation, and it’s cleanest to make another method f(m::Number, v::Number) to contain this.

You can re-use the matrix implementation by writing perhaps f(m::Number, v::Number) = f(hcat(m), [v]) |> only i.e. creating a 1x1 matrix and a vector, calling the first method, and then (perhaps) unwrapping back to a number. But very likely you can do something more efficient working directly with scalars instead.

9 Likes