# Default value of wrong type in function definition

**URL:** <https://discourse.julialang.org/t/default-value-of-wrong-type-in-function-definition/12352>\
**Category:** Internals & Design\
**Created:** [July 13, 2018, 12:38pm UTC](https://discourse.julialang.org/t/default-value-of-wrong-type-in-function-definition/12352 "2018-07-13T12:38:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JonasIsensee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonasisensee/32/4704_2.png) [@JonasIsensee](https://discourse.julialang.org/u/JonasIsensee)\
**Post date:** [July 13, 2018, 12:38pm UTC](https://discourse.julialang.org/t/default-value-of-wrong-type-in-function-definition/12352/1 "2018-07-13T12:38:09Z")

</div>

Hey there,  
We encountered odd behaviour while experimenting. (julia v0.7-beta )  
Of course the type annotation itself wouldn’t be necessary here, but still…

```julia
julia> f(x::Float64=1) = 10
f (generic function with 2 methods)

julia> f(1.)
10

julia> f()
ERROR: MethodError: no method matching f(::Int64)
Closest candidates are:
  f(::Float64) at REPL[1]:1
  f() at REPL[1]:1
Stacktrace:
 [1] f() at ./REPL[1]:1
 [2] top-level scope at none:0

```

I find it confusing that the function definition is accepted in the first place.  
Shouldn’t something like this, i.e. `typeof(default_value) == type_annotation`, be checked?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 13, 2018, 12:53pm UTC](https://discourse.julialang.org/t/default-value-of-wrong-type-in-function-definition/12352/2 "2018-07-13T12:53:22Z")

</div>

This would only be feasible when the default value is a constant, but yes, a warning in these cases could be nice.

Note that of course the compiler is aware of this:

```julia
julia> @code_warntype f()
Body::Union{}
1 1 ─ %%#self#(1) │
  └── unreachable │
  2 ─ unreachable │

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 13, 2018, 1:00pm UTC](https://discourse.julialang.org/t/default-value-of-wrong-type-in-function-definition/12352/3 "2018-07-13T13:00:10Z")

</div>

```julia
f(x::Float64=1) = 10

```

defines two methods:

```julia
f() = f(1)
f(x::Float64) = 10

```

the rest falls out from that.

Relevant issue is [https://github.com/JuliaLang/julia/issues/7357](https://github.com/JuliaLang/julia/issues/7357).
