2 functions, same name, one with scalar and one with list arguments



Hello, what I understood is that multiple dispatch allows to write the same function with different arguments, and then the compiler transforms the different functions in different methods.
I am trying to use this feature for a ods to dictionary converter, where the first function works with an array of sheet names or positions (and returns a dictionary of dictionaries) and, for user’s convenience, a second function that works with a single sheet name/pos and call the first function.

The first function is defined as:
function ods2dic(filename;sheetsNames=[],sheetsPos=[],ranges=[])
The second one is defined as:
function ods2dic(filename;sheetName::String=nothing,sheetPos::Int64=nothing,range=())

The problem is that when I write my calls as:
outDic1 = OdsIO.ods2dic("spreadsheet.ods";sheetsPos=[1,3],ranges=[((1,1),(3,3)),((2,2),(6,4))])
Julia tries to call the second function, and obviously throws an error because different keyword arguments (note the s)… …but the second function should be called only when a single String or Integer is given,
Why (she?) does that ?
(if I comment out the second function, the call is correctly dispatched to the first function)


Julia (“it”!, see community guidelines for why) does not specialize on keyword arguments. so you’d need to change the sheetName/sheetNames argument to be positional so that Julia can distinguish between the two methods.

function ods2dic(filename::AbstractString, sheets::String="";sheetpos=0, range=())
function ods2dic(filename::AbstractString, sheets::AbstractVector;sheetpos=[],ranges=[])

Also, you shouldn’t be assigning nothing to an Int64 typed variable.


I guess the relevant answer is:

Methods are dispatched based only on positional arguments, with keyword arguments processed after the matching method is identified.
(from http://docs.julialang.org/en/release-0.5/manual/methods/)

…I’ll have to revise my functions! :frowning:


Thank you. The problem is that sheetsNames, sheetsPos and ranges are all optional and better handled as keyword arguments. I am better off choosing an other name for the scalar function!


OK, that’s why I put the ="" for the scalar method, so that if you just called with the filename, and maybe some of the optional keyword arguments, it would pick the scalar version with “” for the name.