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

**URL:** https://discourse.julialang.org/t/how-to-specify-type-of-array-keyword-argument-with-default-empty-array/850
**Category:** New to Julia
**Tags:** question
**Created:** [December 10, 2016, 6:44pm UTC](https://discourse.julialang.org/t/how-to-specify-type-of-array-keyword-argument-with-default-empty-array/850 "2016-12-10T18:44:43Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [December 10, 2016, 6:44pm UTC](https://discourse.julialang.org/t/how-to-specify-type-of-array-keyword-argument-with-default-empty-array/850/1 "2016-12-10T18:44:43Z")

</div>

Hello, I would like to write the following function:

```julia
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 ?

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [December 10, 2016, 6:52pm UTC](https://discourse.julialang.org/t/how-to-specify-type-of-array-keyword-argument-with-default-empty-array/850/2 "2016-12-10T18:52:26Z")

</div>

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`).

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [December 10, 2016, 8:14pm UTC](https://discourse.julialang.org/t/how-to-specify-type-of-array-keyword-argument-with-default-empty-array/850/3 "2016-12-10T20:14:34Z")

</div>

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…

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [December 10, 2016, 8:53pm UTC](https://discourse.julialang.org/t/how-to-specify-type-of-array-keyword-argument-with-default-empty-array/850/4 "2016-12-10T20:53:11Z")

</div>

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`.
