imagine the following situation: I have a lot of function that operate on some struct and get a symbol as input (maybe function that do something with a dataframe):
function doA(... , s::Symbol)
function doB(... , s::Symbol)
function doC(... , s::Symbol)
...
Note that doA, doB, … might have different (number of) arguments, only the last argument is always of type Symbol. I would like to have for each function a version that operates on Strings. I could implement each of these functions
function doA(... , s::String) = doA(..., Symbol(s))
function doB(... , s::String) = doB(..., Symbol(s))
function doC(... , s::String) = doC(..., Symbol(s))
...
This leads to a lot of boilerplate code which I would like to avoid. Is there a standard method to deal with such situations?
This may be a bit ugly but works, it does switch ordering though
doA(a,b,c,S::Symbol) = S
doB(a,S::Symbol) = S
doC(a,b,S::Symbol) = S
struct StrFun{F<:Function}
f::F
end
(fstr::StrFun)(s::String,args...) = fstr.f(args...,Symbol(s))
StrFun(doA)("abc",1,2,3)
If s::String or any other input is just for conversion to Symbol before forwarding to the s::Symbol implementation, then why not call this directly? The only reason I would call doA(..., s) is if I expected the type of s to change what methods are dispatched.
If there are more specific s::String methods like that and the Symbol conversion is just the fallback, then this is justified. Duplicate method signatures don’t work in a package, so if you might want that then you’d have to carefully avoid it, especially with metaprogramming. Personally, I’d do so by changing your existing methods to sl::SymbolLike, internally convert s = Symbol(sl), and name the update-able supertype const SymbolLike = Union{Symbol, String}. Then any more specific s::String methods won’t be duplicates.