# Problem with optional argument with default value of missing

**URL:** <https://discourse.julialang.org/t/problem-with-optional-argument-with-default-value-of-missing/48002>\
**Category:** General Usage\
**Created:** [October 8, 2020, 8:52pm UTC](https://discourse.julialang.org/t/problem-with-optional-argument-with-default-value-of-missing/48002 "2020-10-08T20:52:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [October 8, 2020, 8:52pm UTC](https://discourse.julialang.org/t/problem-with-optional-argument-with-default-value-of-missing/48002/1 "2020-10-08T20:52:10Z")

</div>

I have a small package [CurrentPopulationSurvey.jl](https://github.com/mthelm85/CurrentPopulationSurvey.jl) that I’m working on and in it I have the following function defined:

```julia
function cpsdata(year::Int, month::Int, vars::Vector{String}=missing)
    data = readdlm(project_path("data/links_dicts.csv"), ',', skipstart=1)
    registerdep(year, month, data)
    return createtable(year, month, vars, data)
end

```

When I load the package and call `cpsdata` without the optional argument I get a MethodError:

```julia
julia> cpsdata(2020,1)
ERROR: MethodError: no method matching cpsdata(::Int64, ::Int64, ::Missing)
Closest candidates are:
  cpsdata(::Int64, ::Int64, ::Array{String,1}) at C:\Users\mthel\.julia\packages\CurrentPopulationSurvey\PgkXT\src\CurrentPopulationSurvey.jl:74
  cpsdata(::Int64, ::Int64) at C:\Users\mthel\.julia\packages\CurrentPopulationSurvey\PgkXT\src\CurrentPopulationSurvey.jl:74
Stacktrace:
 [1] cpsdata(::Int64, ::Int64) at C:\Users\mthel\.julia\packages\CurrentPopulationSurvey\PgkXT\src\CurrentPopulationSurvey.jl:75
 [2] top-level scope at REPL[25]:1

```

But the method clearly exists:

```julia
julia> methods(cpsdata)
# 2 methods for generic function "cpsdata":
[1] cpsdata(year::Int64, month::Int64, vars::Array{String,1}) in CurrentPopulationSurvey at C:\Users\mthel\.julia\packages\CurrentPopulationSurvey\PgkXT\src\CurrentPopulationSurvey.jl:74
[2] cpsdata(year::Int64, month::Int64) in CurrentPopulationSurvey at C:\Users\mthel\.julia\packages\CurrentPopulationSurvey\PgkXT\src\CurrentPopulationSurvey.jl:74

```

I don’t understand why the second method isn’t being dispatched…? This, for example, works fine:

```julia
function foo(a,b,c=missing)
    return ismissing(c) ? a + b : a + b + c
end

julia> foo(1,2)
3

julia> foo(1,2,3)
6

julia> foo(1,2,missing)
3

```

---

<div class="post-metadata">

**Author:** ![apo383](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apo383/32/11272_2.png) [@apo383](https://discourse.julialang.org/u/apo383)\
**Post date:** [October 8, 2020, 9:14pm UTC](https://discourse.julialang.org/t/problem-with-optional-argument-with-default-value-of-missing/48002/2 "2020-10-08T21:14:07Z")

</div>

Have you tried `vars::Union{Vector{String}, Missing}`? `missing` is a value of type [`Missing`](https://docs.julialang.org/en/v1/manual/missing/), and will not match the expected `Vector{String}`.

Your `foo` example works because the type of `c` is unspecified, and automatically accommodates `Missing`.

Also, you may not need to specify `Int`, which could potentially limit the function’s composability with other packages.

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [October 8, 2020, 9:25pm UTC](https://discourse.julialang.org/t/problem-with-optional-argument-with-default-value-of-missing/48002/3 "2020-10-08T21:25:29Z")

</div>

I’m very embarrassed to not have realized that…been staring at the computer too long. Thanks : )
