# Keyword argument with a default value that depends on a type parameter

**URL:** https://discourse.julialang.org/t/keyword-argument-with-a-default-value-that-depends-on-a-type-parameter/4260
**Category:** General Usage
**Created:** [June 14, 2017, 4:20am UTC](https://discourse.julialang.org/t/keyword-argument-with-a-default-value-that-depends-on-a-type-parameter/4260 "2017-06-14T04:20:59Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![wsshin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsshin/32/360_2.png) [@wsshin](https://discourse.julialang.org/u/wsshin)
#### Post date: [June 14, 2017, 4:21am UTC](https://discourse.julialang.org/t/keyword-argument-with-a-default-value-that-depends-on-a-type-parameter/4260/1 "2017-06-14T04:21:00Z")

</div>

Consider the following test function:

```julia
julia> test_default(x::T, y::R=Base.rtoldefault(R)) where {R<:Real,T<:Union{R,Complex{R}}} = (R, T)
test_default (generic function with 2 methods)

```

When a single complex number `x` is given, this function returns the type `R` of the real part of `x` and the complex type `T` of `x`:

```julia
julia> test_default(1im)
(Int64, Complex{Int64})

julia> test_default(1.0im)
(Float64, Complex{Float64})

```

However, if we make the second argument `y` a keyword argument, the function cannot be defined:

```julia
julia> test_default_kw(x::T; y::R=Base.rtoldefault(R)) where {R<:Real,T<:Union{R,Complex{R}}} = (R, T)
ERROR: UndefVarError: R not defined

```

Is this a bug, or there is a reason for this error?

Edit: here is a simpler breaking example:

```julia
julia> test_default_kw2(x::T; y::R=0) where {R<:Real,T<:Union{R,Complex{R}}} = (R, T)
ERROR: UndefVarError: R not defined

```

---

<div class="post-metadata">

### Author: ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)
#### Post date: [June 14, 2017, 4:32am UTC](https://discourse.julialang.org/t/keyword-argument-with-a-default-value-that-depends-on-a-type-parameter/4260/2 "2017-06-14T04:32:07Z")

</div>

I think the error here is because keyword arguments don’t participate in dispatch nor parameterization; i.e. it doesn’t really make sense to do `; y::R=...` because the list of keyword arguments (their names, types, and default values) are static for a given method.

Which leads to the next question of whether it should rather be an error to try and use a type parameter as the type of a keyword argument.

---

<div class="post-metadata">

### Author: ![wsshin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsshin/32/360_2.png) [@wsshin](https://discourse.julialang.org/u/wsshin)
#### Post date: [June 14, 2017, 4:43pm UTC](https://discourse.julialang.org/t/keyword-argument-with-a-default-value-that-depends-on-a-type-parameter/4260/3 "2017-06-14T16:43:37Z")

</div>

> [@quinnj](#):
>
> I think the error here is because keyword arguments don’t participate in dispatch nor parameterization;

I see. I didn’t know about this. Is this documented somewhere?

---

<div class="post-metadata">

### Author: ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)
#### Post date: [June 14, 2017, 4:56pm UTC](https://discourse.julialang.org/t/keyword-argument-with-a-default-value-that-depends-on-a-type-parameter/4260/4 "2017-06-14T16:56:35Z")

</div>

There’s one little paragraph in this section: [https://docs.julialang.org/en/latest/manual/methods/#Note-on-Optional-and-keyword-Arguments-1](https://docs.julialang.org/en/latest/manual/methods/#Note-on-Optional-and-keyword-Arguments-1), but should probably be featured more prominently [here](https://docs.julialang.org/en/latest/manual/functions/#Keyword-Arguments-1).
