Function signature with vector types

Suppose I have a function which plots timeseries stored in TSFrames.jl TSFrames.

plot_multirowts(x::Vector{TSFrame}}, seriesnames::Vector{String})

Sometimes, such as when subsetting dataframes, my data will have type Vector{Any}. The function does not work with such input. Should I

  1. convert the input data to Vector{TSFrame}} or
  2. use the Vector{Any} type in the function definition, although in this case the user does not know what type of data the function will accept?
1 Like

Just writing Vector works, but unless you need to use dispatch to make specific methods of plot_multirowts that deal with specific kinds of input in different ways, the “most Julian” way of doing this is, I think,

  • don’t use any types in the function signature, just plot_multirowts(x, seriesnames)
  • document your function with a docstring and write tests, indicating what types you function will accept
  • (perhaps) assert/check that the element type can actually be used within the function, e.g. printing a more expicit error message than some MethodError down the line

Besides the unnecessary restriction that you already encountered, another advantage of removing the type annotation here is that it doesn’t exclude other types that might or might not work with your code (this can also be a downside, depends on the situation really).

4 Likes