# Weird error with optional arguments

**URL:** <https://discourse.julialang.org/t/weird-error-with-optional-arguments/47684>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 3, 2020, 6:43am UTC](https://discourse.julialang.org/t/weird-error-with-optional-arguments/47684 "2020-10-03T06:43:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![subramanya\_gautam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/subramanya_gautam/32/8348_2.png) [@subramanya\_gautam](https://discourse.julialang.org/u/subramanya_gautam)\
**Post date:** [October 3, 2020, 6:43am UTC](https://discourse.julialang.org/t/weird-error-with-optional-arguments/47684/1 "2020-10-03T06:43:21Z")

</div>

I am pretty new to julia - and I encountered this error when I was trying to implement a code that takes an optional argument that is a dict.

So I try

```julia
function test_optional(x,y,z=nothing)
   print("$x,$y")
    if z!=nothing
        print(z)
    end
end

```

which gives the expected output

```julia
test_optional (generic function with 2 methods)

```

And it works fine. Now if I add a type to z such as Vector ,

```julia
function test_optional_vector(x,y,z::Vector{Float64}=nothing)
   print("$x,$y")
    if z!=nothing
        print(z)
    end
end

```

Again this leads to a function with 2 methods, however when I try to run this without z

```julia
test_optional_vector(1,2)

```

I get this error,

```julia
MethodError: no method matching test_optional_vector(::Int64, ::Int64, ::Nothing)
Closest candidates are:
  test_optional_vector(::Any, ::Any) at In[32]:1
  test_optional_vector(::Any, ::Any, !Matched::Array{Float64,1}) at In[32]:1

Stacktrace:
 [1] test_optional_vector(::Int64, ::Int64) at ./In[32]:2
 [2] top-level scope at In[33]:1
 [3] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091

```

Any idea what is going on? This is with Julia 1.5

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [October 3, 2020, 6:47am UTC](https://discourse.julialang.org/t/weird-error-with-optional-arguments/47684/2 "2020-10-03T06:47:22Z")

</div>

> [@subramanya\_gautam](#):
>
> `test_optional_vector(x,y,z::Vector{Float64}=nothing)`

The variable `z` is not allowed to be nothing due to the type declaration.  
`test_optional_vector(x,y,z::Union{Vector{Float64}, Nothing}=nothing)` should work.

---

<div class="post-metadata">

**Author:** ![subramanya\_gautam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/subramanya_gautam/32/8348_2.png) [@subramanya\_gautam](https://discourse.julialang.org/u/subramanya_gautam)\
**Post date:** [October 3, 2020, 6:52am UTC](https://discourse.julialang.org/t/weird-error-with-optional-arguments/47684/3 "2020-10-03T06:52:16Z")

</div>

Is there a shorter way to pass typed optional arguments?

Thanks!

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [October 3, 2020, 6:55am UTC](https://discourse.julialang.org/t/weird-error-with-optional-arguments/47684/4 "2020-10-03T06:55:52Z")

</div>

You could use multiple dispatch to manually define 2 methods, one with and one without the parameter. This way, you don’t need the sentinel value `nothing`.  
This is not a 1:1 correspondence, but usually the more “Julian” way.

---

<div class="post-metadata">

**Author:** ![subramanya\_gautam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/subramanya_gautam/32/8348_2.png) [@subramanya\_gautam](https://discourse.julialang.org/u/subramanya_gautam)\
**Post date:** [October 3, 2020, 6:56am UTC](https://discourse.julialang.org/t/weird-error-with-optional-arguments/47684/5 "2020-10-03T06:56:19Z")

</div>

Thanks!  
That would have been my next step  
Subramanya

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [October 3, 2020, 6:58am UTC](https://discourse.julialang.org/t/weird-error-with-optional-arguments/47684/6 "2020-10-03T06:58:30Z")

</div>

In your example, this could be

```julia
test_optional_vector(x,y) = print("$x,$y")
function test_optional_vector(x,y,z::Vector{Float64})
   test_optional_vector(x,y)
   print(z)
end

```
