# How to define keyword argument's type?

**URL:** <https://discourse.julialang.org/t/how-to-define-keyword-arguments-type/2605>\
**Category:** New to Julia\
**Created:** [March 11, 2017, 8:32am UTC](https://discourse.julialang.org/t/how-to-define-keyword-arguments-type/2605 "2017-03-11T08:32:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tejus-gupta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tejus-gupta/32/5575_2.png) [@tejus-gupta](https://discourse.julialang.org/u/tejus-gupta)\
**Post date:** [March 11, 2017, 8:32am UTC](https://discourse.julialang.org/t/how-to-define-keyword-arguments-type/2605/1 "2017-03-11T08:32:40Z")

</div>

![](https://global.discourse-cdn.com/julialang/original/3X/9/a/9a6177359b6ed966549cc00b5e79acc846e83208.png)

Here is simplest example of the problem I am facing. I want to explicitly say that argument b is of type Bool. I have gone through the document but I can’t figure out my mistake.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [March 11, 2017, 8:36am UTC](https://discourse.julialang.org/t/how-to-define-keyword-arguments-type/2605/2 "2017-03-11T08:36:39Z")

</div>

Keyword arguments don’t necessarily dispatch like you think they would. There is a pull request for it.

[https://github.com/JuliaLang/julia/pull/16580](https://github.com/JuliaLang/julia/pull/16580)

---

<div class="post-metadata">

**Author:** ![Evizero](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evizero/32/10118_2.png) [@Evizero](https://discourse.julialang.org/u/Evizero)\
**Post date:** [March 11, 2017, 8:54am UTC](https://discourse.julialang.org/t/how-to-define-keyword-arguments-type/2605/3 "2017-03-11T08:54:11Z")

</div>

I think the confusion is somewhere else here.

First of all, in your example you do correctly specify that `b` should be of type `Bool`. However, you define `b` as a keyword argument, and consequently it needs to be specified as such

```julia
julia> function f(a; b::Bool=true)
           println(b)
       end
f (generic function with 1 method)

julia> f(4, b=false)
false

julia> f(4, b=3)
ERROR: TypeError: non-boolean (Int64) used in boolean context
 in (::#kw##f)(::Array{Any,1}, ::#f, ::Int64) at ./<missing>:0

```

Currently Julia does not behave like R in that regard, so if you want to specify `b` as a positional parameter as well as a keyword argument you need to specify an additional method

```julia
julia> f(4, false)
ERROR: MethodError: no method matching f(::Int64, ::Bool)
Closest candidates are:
  f(::Any; b) at REPL[1]:2

julia> function f(a, b::Bool)
           println(b)
       end
f (generic function with 2 methods)

julia> f(4, false)
false

julia> f(4, 3)
ERROR: MethodError: no method matching f(::Int64, ::Int64)
Closest candidates are:
  f(::Any; b) at REPL[47]:2
  f(::Any, ::Bool) at REPL[50]:2

```

---

<div class="post-metadata">

**Author:** ![tejus-gupta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tejus-gupta/32/5575_2.png) [@tejus-gupta](https://discourse.julialang.org/u/tejus-gupta)\
**Post date:** [March 11, 2017, 9:24am UTC](https://discourse.julialang.org/t/how-to-define-keyword-arguments-type/2605/4 "2017-03-11T09:24:24Z")

</div>

Got it! Thanks for the help 🙂

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [March 11, 2017, 11:28am UTC](https://discourse.julialang.org/t/how-to-define-keyword-arguments-type/2605/5 "2017-03-11T11:28:40Z")

</div>

You can also provide a default value for non-keyword arguments, in which case Julia will automatically generate 2 methods:

```julia
julia> function f(a, b::Bool=true)
                println(b)
          end
f (generic function with 2 methods)

julia> f(42)
true

julia> f(42, false)
false

julia> methods(f)
# 2 methods for generic function "f":
f(a) in Main at REPL[1]:2
f(a, b::Bool) in Main at REPL[1]:2

```
