# Idiomatic way of writing function with all named arguments and no defaults

**URL:** https://discourse.julialang.org/t/idiomatic-way-of-writing-function-with-all-named-arguments-and-no-defaults/131131
**Category:** General Usage
**Created:** [July 28, 2025, 12:57pm UTC](https://discourse.julialang.org/t/idiomatic-way-of-writing-function-with-all-named-arguments-and-no-defaults/131131 "2025-07-28T12:57:33Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [July 28, 2025, 12:57pm UTC](https://discourse.julialang.org/t/idiomatic-way-of-writing-function-with-all-named-arguments-and-no-defaults/131131/1 "2025-07-28T12:57:33Z")

</div>

There are a few very similar threads but often old(ish) so I’ll add another one. What’s the idiomatic way of writing something like this:

```julia-auto
function myfun(; x = nothing, y = nothing, z = nothing)
    @assert !isnothing(x) || !isnothing(y) || !isnothing(z)

    x + y + z
end

```

where I want all arguments to be named at callsite (as it’s easy to mix things up and get silently wrong results) but not have defaults (as again it’s easy to go wrong by forgetting about them).

---

<div class="post-metadata">

### Author: ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)
#### Post date: [July 28, 2025, 1:05pm UTC](https://discourse.julialang.org/t/idiomatic-way-of-writing-function-with-all-named-arguments-and-no-defaults/131131/2 "2025-07-28T13:05:48Z")

</div>

```julia-auto

julia> function myfun(; x, y, z)
           x + y + z
       end
myfun (generic function with 1 method)

julia> myfun()
ERROR: UndefKeywordError: keyword argument `x` not assigned

```

work?

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [July 28, 2025, 1:11pm UTC](https://discourse.julialang.org/t/idiomatic-way-of-writing-function-with-all-named-arguments-and-no-defaults/131131/3 "2025-07-28T13:11:27Z")

</div>

Pretty obvious, thanks!

Do I need to worry about performance with this approach given the function is called through all kwargs?

---

<div class="post-metadata">

### Author: ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)
#### Post date: [July 28, 2025, 1:20pm UTC](https://discourse.julialang.org/t/idiomatic-way-of-writing-function-with-all-named-arguments-and-no-defaults/131131/4 "2025-07-28T13:20:07Z")

</div>

I’m not aware of any known (or common) performance holes with kwargs, but maybe it’s possible someone is able to cook up some obscure exceptions. I predict the main challenge you’ll face with this kind of API is that the error messages will tend to be worse and less helpful since they don’t always thread through the kwcall gracefully

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [July 28, 2025, 1:29pm UTC](https://discourse.julialang.org/t/idiomatic-way-of-writing-function-with-all-named-arguments-and-no-defaults/131131/5 "2025-07-28T13:29:49Z")

</div>

`::Type{}` arguments end up as type-unstable `::DataType` fields in a `NamedTuple` argument to the lowered underlying function, otherwise it does as well as with positional arguments. Similar downside happens for closures capturing variables assigned to types.

---

<div class="post-metadata">

### Author: ![technocrat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/technocrat/32/220947_2.png) [@technocrat](https://discourse.julialang.org/u/technocrat)
#### Post date: [July 29, 2025, 8:34pm UTC](https://discourse.julialang.org/t/idiomatic-way-of-writing-function-with-all-named-arguments-and-no-defaults/131131/6 "2025-07-29T20:34:53Z")

</div>

Three parameters, none of which have a default?

---

<div class="post-metadata">

### Author: ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)
#### Post date: [July 29, 2025, 8:51pm UTC](https://discourse.julialang.org/t/idiomatic-way-of-writing-function-with-all-named-arguments-and-no-defaults/131131/7 "2025-07-29T20:51:09Z")

</div>

> [@technocrat](#):
>
> Three parameters, none of which have a default?

indeed, as the OP specified

> [@nilshg](#):
>
> I want all arguments to be named at callsite […] but not have defaults
