# Check for undefined keyword

**URL:** <https://discourse.julialang.org/t/check-for-undefined-keyword/20193>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 28, 2019, 7:42pm UTC](https://discourse.julialang.org/t/check-for-undefined-keyword/20193 "2019-01-28T19:42:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [January 28, 2019, 7:42pm UTC](https://discourse.julialang.org/t/check-for-undefined-keyword/20193/1 "2019-01-28T19:42:22Z")

</div>

just stumbled upon julia’s keyword mechanism

```julia
julia> func(a; b::B) where B = a
func (generic function with 1 method)

julia> func(1)
ERROR: UndefKeywordError: keyword argument b not assigned
Stacktrace:
 [1] func(::Int64) at .\none:1
 [2] top-level scope at none:0

```

To me it seems like it would make more sense to let `b` undefined until either `b` or `B` is accessed.  
There already is `isundefined` method which could be used to check this.

---

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [January 28, 2019, 7:45pm UTC](https://discourse.julialang.org/t/check-for-undefined-keyword/20193/2 "2019-01-28T19:45:52Z")

</div>

interestingly when defining both

```julia
func(a; b::B) where B = a
func(a) = a + 1

```

then both versions work

```julia
julia> func(1)
2
julia> func(1; b = 2)
1

```

however when used in a package, it prints a `WARNING: Method definition ... overwritten at ...` despite nothing seems overwritten

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [January 28, 2019, 8:59pm UTC](https://discourse.julialang.org/t/check-for-undefined-keyword/20193/3 "2019-01-28T20:59:24Z")

</div>

Keyword arguments don’t take part in dispatch, that’s why “Method overriden” is being printed.

As for why it “works” anyway, my best guess is that the _presence_ of keyword arguments still gives enough information to distinguish the two versions in this case. I can’t check right now, but I bet if you define another method with a different keyword, dispatching on the method with one keyword won’t work anymore.

---

<div class="post-metadata">

**Author:** ![jeff.bezanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff.bezanson/32/48_2.png) [@jeff.bezanson](https://discourse.julialang.org/u/jeff.bezanson)\
**Post date:** [January 28, 2019, 9:37pm UTC](https://discourse.julialang.org/t/check-for-undefined-keyword/20193/4 "2019-01-28T21:37:10Z")

</div>

Correct; this has been a quasi-bug for a while ([Keyword arguments affect methods dispatch · Issue #9498 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/9498)). Might change in 2.0.

As for the original question, we decided it was more useful to require the caller to pass a certain keyword argument. Otherwise we would need a lot of boilerplate `if !@isdefined(b); error("you must pass b");` etc. Given that though, it looks like the error message should be reworded, something like “keyword argument b must be specified by callers”. The current behavior is also a little easier to implement, since you can’t pass around undefined variables.
