I am not sure I understand what you want but I think in this case multiple dispatch is your friend:
# First method
function myfun(x::Coords)
# Do something with a single element
end
# Second method
function myfun(x::Vector{Coords})
# Do something with an array
end
I am dealing with an unknown number of types (which could be higher dimensional as well).
So I would prefer to have a test like is_single/is_vector/is_matrix.
Note that is_single(1) == is_single([1]) == false, and that this is quite slow. Dispatch on f(x::AbstractArray) vs. f(x) will I think do what you want. Or x isa AbstractArray if you want a test within a function:
I rarely come across situations where I don’t know if I’m dealing with a scalar or an array without running the code. This is also why explicit syntactic broadcasting is preferred over dispatching to different methods for scalar vs array (except when you need non-broadcasting behavior, e.g. matrix multiplication).
I suspect a code smell here. Where do these arrays come from, and why can’t you tell if objects are scalar or arrays without running the code?
I’m going to second this. This situation usually comes up from new Julia users coming from Matlab, and the real solution usually is to use broadcasting and not quite vector/matrix versions of your functions.
To elaborate on this, Matlab (and Python and R) want you to push vectorized operations “in” as far as possible so that you can pass large arrays into the primitive operations that are written in fast C code, allowing them to amortize over the entire array and try to make up for the slowness of the outer language. Julia wants you to do the opposite: push vectorization “out” as far as possible and leave it to the very last moment. Express your core operations in terms of scalars and then only do broadcasting when you need to. This works fine because the outer language is fast in itself and doesn’t need you to force everything into vectorized operations. In fact, this approach is typically more efficient since it uses less memory and has better memory locality by combining multiple operations on the same data instead of having to make many passes over the same data computing a stage at a time as Matlab, Python and R would.
Coming from Matlab and translating a Matlab serializer code.
To be specific, a Julia serializer translating into Matlab compatible structures, so departing from a MAT.jl approach using HDF as intermediate format.
So of course I know what is single or Vector, just a bit hesitant to accept multiple dispatch as the panacea. If not sufficiently generalizable to few types, I imagine a lot of code replication. There might be a threshold by the number of ifs to convert to MD though.
Thanks for giving a hand, maybe these little bits and pieces on discourse make it some day into the manual.
If you run into any specific bits that feel weird in Julia, feel free to post an example. Usually, the answer is just to use broadcasting, but the specifics are sometimes a little more complicated/require rethinking the approach a little.