# Concise call keyword arg with default by conditional

**URL:** <https://discourse.julialang.org/t/concise-call-keyword-arg-with-default-by-conditional/90716>\
**Category:** General Usage\
**Tags:** argument, functions, keywords\
**Created:** [November 23, 2022, 3:21pm UTC](https://discourse.julialang.org/t/concise-call-keyword-arg-with-default-by-conditional/90716 "2022-11-23T15:21:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![taotree](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/taotree/32/31982_2.png) [@taotree](https://discourse.julialang.org/u/taotree)\
**Post date:** [November 23, 2022, 3:21pm UTC](https://discourse.julialang.org/t/concise-call-keyword-arg-with-default-by-conditional/90716/1 "2022-11-23T15:21:23Z")

</div>

I tried to show an example below. This might be a bit persnickety, but it seems like it would be nice to have a cleaner solution for this situation.

I have function A with a keyword argument that specifies a default value. I have an “in between” function B that receives a value and calls function A with that value. However, if that value is nothing, it should use the default value. Is there some way to code this without what seems like redundancy?

Either I have to write a conditional at the call site, or have function A have both a default value for the argument (I need that for other cases) and convert a nothing value to that default value inside the function.

```julia
function test(x; kwarg=100)
  println(x, ' ', arg2)
end

a = nothing

# How to remove the conditional so it uses default value for kwarg?
isnothing(a) ? test(17) : test(17; kwarg=a)

```

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [November 23, 2022, 4:28pm UTC](https://discourse.julialang.org/t/concise-call-keyword-arg-with-default-by-conditional/90716/2 "2022-11-23T16:28:19Z")

</div>

Interesting problem. Not at a computer right now, but does `test(17; (isnothing(a) ? (; kwarg=a) : (;))...)` do anything?

---

<div class="post-metadata">

**Author:** ![halleysfifthinc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/halleysfifthinc/32/206280_2.png) [@halleysfifthinc](https://discourse.julialang.org/u/halleysfifthinc)\
**Post date:** [November 23, 2022, 5:04pm UTC](https://discourse.julialang.org/t/concise-call-keyword-arg-with-default-by-conditional/90716/3 "2022-11-23T17:04:05Z")

</div>

> [@taotree](#):
>
> Either I have to write a conditional at the call site, or have function A have both a default value for the argument (I need that for other cases) and convert a nothing value to that default value inside the function.

The latter is the idiomatic/Julian solution: assign `kwarg=nothing` in the signature, and in the function body, and set `kwarg` to its default value if `isnothing(kwarg)`. `nothing` serves as an “unset” sentinel value that doesn’t special case a regular value (e.g. using `-1` as the “unset” value). `nothing` could even still be used as a valid value by giving the `kwarg` as `Some(nothing)`.

```julia
function A(x; kwarg=nothing)
    if isnothing(kwarg)
        kwarg = 100
    end
    # rest of function
end

```
