How to specify type of array keyword argument with default empty array?

question

#1

Hello, I would like to write the following function:

function ods_readall(filename::String;sheetsNames=[],sheetsPos[],ranges=[],innerType="Matrix")

and I would like to specify that filename must be a String, sheetNames (when given) an Array{String,1}, sheetsPos (when given) an Array{Int64,1}, range (when given) an Array{Tuple{Tuple{Int64,Int64},Tuple{Int64,Int64}},1} and finally innerType (when given) a String

The problem is with the arrays. If, for example, I specify the sheetNames::Array{String,1}=[] and I use the function with sheetsNames, all fine.
But if I then call it using instead sheetsPos I got a no method matching error.

So, how to type-define array optional arguments ?


#2

It’s not specific to optional argument. To create an empty array of certain eltype T, use T[].

Note that if this is an public function, you should not specify the type parameters to be this restricted. You should use abstract types instead (at least AbstractVector and AbstractString).


#3

Thank you for the quick answer.
I do not understand however:
(a) why Array{String,1} and Array{Int64,1} would be too restrictive ?
(b) which is the syntax to apply to array-type optional arguments with null value by default ?
if I use function ods_readall(...,sheetsNames::AbstractVector[],sheetsPos::AbstractVector[],...) it doesn’t even compile…

EDIT:
Ok, I got the syntax: sheetsNames::Array{String,1}=String[] sorry, I got dumb…
But still (a) remains…


#4

It’ll prevent people from using it on other types that would have worked. E.g. a SubArray or a SubString cannot be accepted if the function can only accept Array or String.