I have looked at the docs for parametric args in functions and cannot understand how to apply it to what I want to program.
I have a function function ga(s::String,g::T) where ::T can be a ::String or a ::Array{Num} and have an if statement in ga that does on thing for ::String and another for ::Array{Num}. Because there is a lot of common code for each case I don’t want to define two functions (in the g::String case I have code to convert g::String to g::Array{Num}) -
function ga(s::String,g::String)
function ga(s::String,g::Array{Num})
Is there a way I can do this with parametric arguments (multiple dispatch)? Thank you for any help you can give me.
Because there is a lot of common code for each case I don’t want to define two functions
You don’t have to, you can dispatch somewhere in the function, too.
function ga(s::String, g)
g = make_array(g)
# other code
end
make_array(g::Array{Num}) = g
make_array(g::String) = [1, 2, 3]
In terms of compilation latency, it’s best to do all conversions towards the unified format first, then dispatch to the “real” function doing all the work. Then that one will not be compiled again and again.
const G = Union{String, Array{Num}}
function ga(s::String,g::G)
...
end
# alternatively, to specialize on a type
function ga(s::String, g::T) where T <: G
...
end