# Default value that doesn't correspond to an argument type

**URL:** https://discourse.julialang.org/t/default-value-that-doesnt-correspond-to-an-argument-type/74212
**Category:** New to Julia
**Tags:** question
**Created:** [January 7, 2022, 7:42pm UTC](https://discourse.julialang.org/t/default-value-that-doesnt-correspond-to-an-argument-type/74212 "2022-01-07T19:42:15Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![serhii](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/serhii/32/13719_2.png) [@serhii](https://discourse.julialang.org/u/serhii)
#### Post date: [January 7, 2022, 7:42pm UTC](https://discourse.julialang.org/t/default-value-that-doesnt-correspond-to-an-argument-type/74212/1 "2022-01-07T19:42:15Z")

</div>

Hi! Can someone explain why julia compiler allows the following function declaration:

```julia
fun(arg::String=nothing) = arg

```

Which is quite confusing especially considering that julia itself claims that there are two methods for the function `fun`:

```julia
julia> methods(fun)
# 2 methods for generic function "fun":
[1] fun() in Main at REPL[10]:1
[2] fun(arg::String) in Main at REPL[10]:1

```

However, if one attempts to call `fun()` there is an exception

```julia
julia> fun()
ERROR: MethodError: no method matching fun(::Nothing)
Closest candidates are:
  fun() at REPL[10]:1
  fun(::String) at REPL[10]:1
Stacktrace:
 [1] fun()
   @ Main ./REPL[10]:1
 [2] top-level scope
   @ REPL[11]:1

```

---

<div class="post-metadata">

### Author: ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)
#### Post date: [January 7, 2022, 7:47pm UTC](https://discourse.julialang.org/t/default-value-that-doesnt-correspond-to-an-argument-type/74212/2 "2022-01-07T19:47:54Z")

</div>

The default value is evaluated every time the function is called without an argument (and that’s a good thing, consider the common `output=[]` mistake in Python). It would be incorrect to output an error at function creation time:

```julia
julia> f(arg::String=nothing) = arg
f (generic function with 2 methods)

julia> nothing = "hello"
"hello"

julia> f()
"hello"

```
