# Deprecating usage of keywords

**URL:** https://discourse.julialang.org/t/deprecating-usage-of-keywords/978
**Category:** General Usage
**Created:** [December 15, 2016, 8:03pm UTC](https://discourse.julialang.org/t/deprecating-usage-of-keywords/978 "2016-12-15T20:03:41Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![pkofod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pkofod/32/2179_2.png) [@pkofod](https://discourse.julialang.org/u/pkofod)
#### Post date: [December 15, 2016, 8:03pm UTC](https://discourse.julialang.org/t/deprecating-usage-of-keywords/978/1 "2016-12-15T20:03:41Z")

</div>

In Optim ([https://github.com/JuliaOpt/Optim.jl/pull/304](https://github.com/JuliaOpt/Optim.jl/pull/304)) I am currently deprecating our old keyword based options system in favor of the `Optim.Options` (earlier `OptimizationOptions`) approach.

Say I used to have the following methods

```julia
function myfun(a, b, c, o)
    ... do stuff ...
end
function myfun(a, b, c; d = 1, e = 2)
    myfun(a, b, c, Options(d = d, e = e))
end
function myfun(a, b; d = 1, e = 2)
    myfun(a, b, C(), Options(d = d, e = e)
end

```

Now, to avoid breaking too much code in the wild, I want to deprecate it. In the future, I want

```julia
function myfun(a, b, c, o)
    ... do stuff ...
end
function myfun(a, b, c)
    myfun(a, b, c, Options()
end
function myfun(a, b)
    myfun(a, b, C(), Options()
end

```

My problem is: how do I tell someone who runs `myfun(A(), B(); d = 44)` that I want them to use `myfun(A(), B(), Options(d = 44)`? If I deprecate `myfun(a, b; kwargs...)` it will also throw the warning when people use `myfun(a, b)`, and that is not what I want!

Also, in some other code unrelated to “myfun” (which is actually `optimize`) I want to deprecate the name of a keyword, how do I do that? I want to deprecate `ABC(; keyword! = default_value)` in favor of `ABC(; keyword = default_value)`.

Thanks!

---

<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: [December 16, 2016, 9:07am UTC](https://discourse.julialang.org/t/deprecating-usage-of-keywords/978/2 "2016-12-16T09:07:04Z")

</div>

Perhaps something custom:

```nohighlight
julia> const has_deprecated_kw_old = Ref(false)
Base.RefValue{Bool}(false)

julia> function f(;kw_new = nothing, kw_old = nothing)
           if kw_old !== nothing
               kw_new = kw_old
               if !has_deprecated_kw_old[]
                   warn("kw_old keyword is deprecated, please use kw_new")
                   has_deprecated_kw_old[] = true
               end
           end
           print(kw_new)
       end
f (generic function with 1 method)

julia> f(kw_old="foo")
WARNING: kw_old keyword is deprecated, please use kw_new
foo
julia> f(kw_old="foo")
foo
julia> f(kw_new="foo")
foo

```

---

<div class="post-metadata">

### Author: ![pkofod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pkofod/32/2179_2.png) [@pkofod](https://discourse.julialang.org/u/pkofod)
#### Post date: [December 16, 2016, 9:13am UTC](https://discourse.julialang.org/t/deprecating-usage-of-keywords/978/3 "2016-12-16T09:13:31Z")

</div>

Ah, of course. I thought about checking the value of the old keyword, but didn’t think about changing it to `nothing`. When I thought about it, I wanted to check `old_kw == old_default_value`, but that won’t fire a warning if someone manually sets that value. Since no one should ever set the linesearch kw to nothing, this would work. Thanks
