Default value that doesn't correspond to an argument type

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

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> 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> fun()
ERROR: MethodError: no method matching fun(::Nothing)
Closest candidates are:
  fun() at REPL[10]:1
  fun(::String) at REPL[10]:1
 [1] fun()
   @ Main ./REPL[10]:1
 [2] top-level scope
   @ REPL[11]:1

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> f(arg::String=nothing) = arg
f (generic function with 2 methods)

julia> nothing = "hello"

julia> f()
1 Like