# Allow use of named-argument syntax for positional arguments?

**URL:** <https://discourse.julialang.org/t/allow-use-of-named-argument-syntax-for-positional-arguments/5287>\
**Category:** Internals & Design\
**Tags:** proposal\
**Created:** [August 8, 2017, 7:09pm UTC](https://discourse.julialang.org/t/allow-use-of-named-argument-syntax-for-positional-arguments/5287 "2017-08-08T19:09:15Z")\
**Posts on this page:** 15\
**Page:** 3

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [April 29, 2020, 2:38pm UTC](https://discourse.julialang.org/t/allow-use-of-named-argument-syntax-for-positional-arguments/5287/41 "2020-04-29T14:38:48Z")

</div>

> [@akdor1154](#):
>
> I would love to see this allowed in Julia

This feature already exists. Here’s the syntax:

```julia
function f(; person::String, from::String)
    hometown = from
    return "Hello, $(person)! Glad you could visit from $hometown."
end

julia> f(person = "Bill", from = "Cupertino")
"Hello, Bill! Glad you could visit from Cupertino."

```

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [April 29, 2020, 3:03pm UTC](https://discourse.julialang.org/t/allow-use-of-named-argument-syntax-for-positional-arguments/5287/42 "2020-04-29T15:03:49Z")

</div>

> [@akdor1154](#):
>
> What I have in mind is:
> 
> 1. choose a method implementation via existing dispatch rules
> 2. if the user has provided argument labels, and they don’t match the argument labels in the API, error.
> 
> Is there ambiguity in here that I have missed?

The trouble with this is that you need to know which arguments passed with `key = val` syntax are positional and which are keyword arguments before you can choose a method. But you don’t know that until you’ve selected a method. For example, if I see `f(x = 1, y = 2, z = 3)` how do I know which subset of `x`, `y` and `z` are positional and should be used to dispatch `f` and which are keywords and should be ignored during dispatch? There are 8 = 2^3 possibilities to consider and that number grows exponentially with the number of arguments.

As a higher level observation, Swift was designed to appeal to and make sense to a large existing base of Objective-C programmers and Objective-C does some very unusual stuff with keyword names and dispatch. When someone writes `[obj name:argument]` in Objective-C, the `name` is the name of the method that’s sent to `obj`. So the “keyword names” are actually the name of the method to invoke—any dispatch that’s done happens after looking at the set of keyword names. That’s kind of the opposite of what Julia does, where the dispatch is done ignoring keyword arguments and then keyword values are used to pass additional data to the selected method as though they’d just been assigned at the top of the method body.

More generally when it comes to APIs, if the arguments to a function need labels in order for the call’s meaning to be clear, then the API should probably be reconsidered. Either the arguments should be keywords so that the name is required, or as has been suggested, you could use a structure to hold the options.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [April 29, 2020, 4:27pm UTC](https://discourse.julialang.org/t/allow-use-of-named-argument-syntax-for-positional-arguments/5287/43 "2020-04-29T16:27:41Z")

</div>

I’m going to second what Stefan just said. It seems like the proposal _mostly_ centers around wanting functions to be called with explicitly named arguments. You can already do this with keyword arguments.

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [April 29, 2020, 6:21pm UTC](https://discourse.julialang.org/t/allow-use-of-named-argument-syntax-for-positional-arguments/5287/44 "2020-04-29T18:21:46Z")

</div>

I understand the reasons given against named arguments syntax and don’t have a solution for them. However, the current situation definitely has some rough edges and inconsistencies that would not arise at all if naming any arguments was allowed. Several examples from widely used mature libraries:

```julia
# inconsistency when it would make lots of sense to allow both variants:
mean([...], dims=1) # OK
mean([...], 1) # error
# vs
splitdims([...], dims=1) # error
splitdims([...], 1) # OK

```

```julia
# does it read from a and write to b, or the other way?
map!(x -> x * 2, a, b)
# wouldn't it be better?
map!(x -> x * 2, src=a, dest=b)

# which function is mapping, which is reduction?
mapreduce(f, g, [...])

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 30, 2020, 6:09am UTC](https://discourse.julialang.org/t/allow-use-of-named-argument-syntax-for-positional-arguments/5287/45 "2020-04-30T06:09:27Z")

</div>

There are several ways that the API in Base could be made more consistent — this was done prior to 0.7/1.0 and it can be done again for 2.0. You may want to check if there is an existing issue for things you notice, and if not, open one and ask for a 2.0 milestone.

That said, in

> [@aplavin](#):
>
> ```julia
> # does it read from a and write to b, or the other way?
> map!(x -> x * 2, a, b)
> 
> ```

the convention for `!` is to modify the first argument, while

> [@aplavin](#):
>
> ```julia
> # which function is mapping, which is reduction?
> mapreduce(f, g, [...])
> 
> ```

the API is consistent with the function name (`f` is for mapping, `g` is reducing).

---

<div class="post-metadata">

**Author:** ![akdor1154](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akdor1154/32/15975_2.png) [@akdor1154](https://discourse.julialang.org/u/akdor1154)\
**Post date:** [May 1, 2020, 1:05am UTC](https://discourse.julialang.org/t/allow-use-of-named-argument-syntax-for-positional-arguments/5287/46 "2020-05-01T01:05:26Z")

</div>

> [@StefanKarpinski](#):
>
> The trouble with this is that you need to know which arguments passed with `key = val` syntax are positional and which are keyword arguments before you can choose a method. But you don’t know that until you’ve selected a method. For example, if I see `f(x = 1, y = 2, z = 3)` how do I know which subset of `x` , `y` and `z` are positional and should be used to dispatch `f` and which are keywords and should be ignored during dispatch?

This makes perfect sense, thanks for the detailed analysis. I guess you could use a different syntax for naming positional arguments but that would be pretty ugly.

> [@StefanKarpinski](#):
>
> More generally when it comes to APIs, if the arguments to a function need labels in order for the call’s meaning to be clear, then the API should probably be reconsidered.

This is really the crux of the issue - what is “clear” or not is both hugely subjective, and dependent on context. The current situation means we have a code-style decision relating to consumer code, but it is needing to be decided by API authors, not API consumers.

There’s also the issue that even if an API author decides she wants to force all her consumers to write akdor1154-style code with kwargs everywhere, and is OK with irritating the rest of the community in doing so, she cannot use dispatch on kwargs.

Both @CameronBieganek’s fondness of concise mathematical-style functions and @aplavin’s dislike of perceived ambiguity are perfectly valid, and I’d love to work towards a solution where both of these desires can be satisfied.

---

<div class="post-metadata">

**Author:** ![akdor1154](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akdor1154/32/15975_2.png) [@akdor1154](https://discourse.julialang.org/u/akdor1154)\
**Post date:** [June 27, 2020, 12:16am UTC](https://discourse.julialang.org/t/allow-use-of-named-argument-syntax-for-positional-arguments/5287/47 "2020-06-27T00:16:17Z")

</div>

@StefanKarpinski I’ve had this in the back of my mind for a while - a solution to the problem you raise would be as simple as requiring callers who want to give kws to positional args put a semi in, wouldn’t it? So I as a caller would have to do f(x=4, y=5; z=6) to pass x,y as named positionals. Seems reasonable to me, given everyone really has to be aware about the diff between pos and kwargs anyway.  
You’d probably also need to require a trailing semi for the case of named posargs and no kwargs.

---

<div class="post-metadata">

**Author:** ![akdor1154](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akdor1154/32/15975_2.png) [@akdor1154](https://discourse.julialang.org/u/akdor1154)\
**Post date:** [June 29, 2020, 6:09am UTC](https://discourse.julialang.org/t/allow-use-of-named-argument-syntax-for-positional-arguments/5287/48 "2020-06-29T06:09:10Z")

</div>

I’ve had a shot at a macro implementation of this as per Tamas’ suggestion above: [https://github.com/akdor1154/NamedPositionals.jl](https://github.com/akdor1154/NamedPositionals.jl)

```nohighlight
@np train_model(myModel, max_iters=100, k_weight=0.4, j_weight=0.2;)

```

I’ve not yet published it to the registry but will do so as soon as I iron out a couple more rough edges. Please have a play! There will definitely be situations where you can break it 🙂 Hopefully some of them are fixable…

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [June 29, 2020, 7:34am UTC](https://discourse.julialang.org/t/allow-use-of-named-argument-syntax-for-positional-arguments/5287/49 "2020-06-29T07:34:48Z")

</div>

Looks great! I also agree with this:

> Allowing the caller to re-order arguments: nope. Argument order is hugely important in Julia, it shouldn’t be hidden or abstracted. Hiding this is potentially a huge footgun, and would yield major WTFs from anybody who ever reads your code.
> 
> (from your readme)

Basically, this should work as just an extra check to confirm that argument names match?

---

<div class="post-metadata">

**Author:** ![akdor1154](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akdor1154/32/15975_2.png) [@akdor1154](https://discourse.julialang.org/u/akdor1154)\
**Post date:** [June 30, 2020, 12:42am UTC](https://discourse.julialang.org/t/allow-use-of-named-argument-syntax-for-positional-arguments/5287/50 "2020-06-30T00:42:57Z")

</div>

> [@aplavin](#):
>
> Basically, this should work as just an extra check to confirm that argument names match?

Exactly right.

---

<div class="post-metadata">

**Author:** ![josePereiro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josepereiro/32/17322_2.png) [@josePereiro](https://discourse.julialang.org/u/josePereiro)\
**Post date:** [July 24, 2020, 9:23pm UTC](https://discourse.julialang.org/t/allow-use-of-named-argument-syntax-for-positional-arguments/5287/51 "2020-07-24T21:23:20Z")

</div>

Maybe, at least, just allow some kind of comment in the positional argument list.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [July 24, 2020, 9:52pm UTC](https://discourse.julialang.org/t/allow-use-of-named-argument-syntax-for-positional-arguments/5287/52 "2020-07-24T21:52:50Z")

</div>

That is already possible and folks regularly do so. Here’s a shoddy regex search that pulls up a bunch of hits:

[https://juliahub.com/ui/RepoSearch?q=,%20%23%3D\s\*\w%2B\s\*%3D%23\s\*\w%2B,&r=true](https://juliahub.com/ui/RepoSearch?q=,%20%23%3D%5Cs%2a%5Cw%2B%5Cs%2a%3D%23%5Cs%2a%5Cw%2B,&r=true)

For example:

```julia
close(getfield(socket, :pollfd), #=readable=#true, #=writable=#false)

```

---

<div class="post-metadata">

**Author:** ![ctkelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ctkelley/32/10684_2.png) [@ctkelley](https://discourse.julialang.org/u/ctkelley)\
**Post date:** [July 24, 2020, 11:24pm UTC](https://discourse.julialang.org/t/allow-use-of-named-argument-syntax-for-positional-arguments/5287/53 "2020-07-24T23:24:16Z")

</div>

I don’t think you can do this. The kwargs allow it, but anything before the semicolon does not.

I get bitten by this all the time. I am trying to shape up.

---

<div class="post-metadata">

**Author:** ![akdor1154](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akdor1154/32/15975_2.png) [@akdor1154](https://discourse.julialang.org/u/akdor1154)\
**Post date:** [July 25, 2020, 4:40am UTC](https://discourse.julialang.org/t/allow-use-of-named-argument-syntax-for-positional-arguments/5287/54 "2020-07-25T04:40:30Z")

</div>

FYI this is now published to the registry as a prerelease, version 0.1.0 (and hence can be used by `] add NamedPositionals`) I’ve still got some playing around to do before a proper ANN.

---

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [July 12, 2025, 5:59am UTC](https://discourse.julialang.org/t/allow-use-of-named-argument-syntax-for-positional-arguments/5287/55 "2025-07-12T05:59:22Z")

</div>

Thank you for `NamedPositionals`.

Even when I write my functions and use them myself and even when I think positional parameters make more sense than keyword arguments, I sometimes (not always) want to name the arguments on the calling site, to make sure I get the order right or as a documentation or reminder.

Very often (not always) I have a weak anxiety about positional arguments: “Am I getting the order right”? `NamedPositionals` help ease the anxiety often, if not always. (For example, if the names are abstract such as `x` and `y`, names may not make sufficient distinctions.)

[Previous page](https://discourse.julialang.org/t/allow-use-of-named-argument-syntax-for-positional-arguments/5287.md?page=2)
