# Yet Another named arguments prototype

**URL:** <https://internals.rust-lang.org/t/yet-another-named-arguments-prototype/12937>\
**Category:** language design\
**Created:** [August 20, 2020, 11:00pm UTC](https://internals.rust-lang.org/t/yet-another-named-arguments-prototype/12937 "2020-08-20T23:00:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![illicitonion](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/illicitonion/32/3886_2.png) [@illicitonion](https://internals.rust-lang.org/u/illicitonion)\
**Post date:** [August 20, 2020, 11:00pm UTC](https://internals.rust-lang.org/t/yet-another-named-arguments-prototype/12937/1 "2020-08-20T23:00:22Z")

</div>

Inspired by [@Yoric's interesting experiments](https://internals.rust-lang.org/t/possibly-one-small-step-towards-named-arguments/12857) I set out to see how un-obtrusive and zero-cost I could make a procedural macro implementation. The best I got was a single attribute, and having to throw a `!` in at each call-site.

I put together the [named](https://crates.io/crates/named) crate. Sample code:

```rust
use named::named;

#[named]
fn or(a: bool, b: bool) -> bool {
    a || b
}

fn main() {
    or!(a = true, b = false);
}

```

It also supports optional default values:

```rust
use named::named;

#[named(defaults(b = false))]
fn or(a: bool, b: bool) -> bool {
    a || b
}

fn main() {
    or!(a = true);

    or!(a = true, b = true);
}

```

The error messages are decent, too; [see some trybuild test cases for examples](https://github.com/illicitonion/named/tree/master/tests/compile_fail).

I think this shows that such a feature could reasonably live outside the language. Interesting limitations we would want to overcome if we wanted to turn this into something more real:

- No support for functions defined in `impl` blocks. Attribute macros aren't allowed on `fn`s inside `impl` blocks. This could probably be worked around by having the attribute macro on the containing item, and a helper attribute on the function, but really, it would be nicer to not need to jump through those hoops. I'm not sure why this restriction is in place, I guess to avoid needing to provide surrounding context to the macro? Adding this support could probably help with other use-cases, too.
- No support for functions which take receivers (i.e. `self`). [Postfix macros](https://github.com/rust-lang/rfcs/pull/2442) could potentially help here.
- Macros can't be defined as members of `Struct`s - you can't define a macro such that `Foo::macro!()` can be called. This feels reasonably easily fixable if we wanted to.
- Error messages are a bit limited in terms of context collection. To avoid a combinatorial explosion of match conditions on the macro, error messages if you give arguments in the wrong order, or give multiple unknown arguments, only mention the first problem they encounter. This is the only thing that pushes me towards thinking if we wanted to make this something real it should be done in the language, where more complex matching logic can be written. That said, there are probably more clever things this macro _could_ do, or features we could introduce to the macro system to support this better, and also the error messages aren't terrible, just not as rich as they could be.

But in general, I dislike this interface less than I was expecting to...! I'd be interested to know what other people think.

---

<div class="post-metadata">

**Author:** ![samsieber](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/samsieber/32/4671_2.png) [@samsieber](https://internals.rust-lang.org/u/samsieber)\
**Post date:** [August 21, 2020, 12:40am UTC](https://internals.rust-lang.org/t/yet-another-named-arguments-prototype/12937/2 "2020-08-21T00:40:33Z")

</div>

That's awesome. Thanks for implementing it 😀

> [@illicitonion](#):
>
> - No support for functions which take receivers (i.e. `self` ). [Postfix macros](https://github.com/rust-lang/rfcs/pull/2442) could potentially help here.
> - Macros can't be defined as members of `Struct` s - you can't define a macro such that `Foo::macro!()` can be called. This feels reasonably easily fixable if we wanted to.

If I understand correctly, macros are resolved before type inference which unfortunately makes that difficult ☹

What type of macro do you generate?

I'm betting the macro impl block limitation has to do with generics, but I'm not sure.

---

<div class="post-metadata">

**Author:** ![mcy](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mcy/32/6512_2.png) [@mcy](https://internals.rust-lang.org/u/mcy)\
**Post date:** [August 21, 2020, 1:43pm UTC](https://internals.rust-lang.org/t/yet-another-named-arguments-prototype/12937/3 "2020-08-21T13:43:02Z")

</div>

No reason that `$path::$ident!()` can't desugar to `$ident!($path);` when `$path` doesn't name a module; macros are AFAIK resolved after some amount of HIR name resolution.

---

<div class="post-metadata">

**Author:** ![tesuji](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tesuji/32/12790_2.png) [@tesuji](https://internals.rust-lang.org/u/tesuji)\
**Post date:** [August 21, 2020, 2:35pm UTC](https://internals.rust-lang.org/t/yet-another-named-arguments-prototype/12937/4 "2020-08-21T14:35:58Z")

</div>

Ambiguous syntax for parsing.

> [@mcy](#):
>
> No reason that `$path::$ident!()` can't desugar to `$ident!($path);` when `$path` doesn't name a module;

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [August 21, 2020, 5:40pm UTC](https://internals.rust-lang.org/t/yet-another-named-arguments-prototype/12937/5 "2020-08-21T17:40:59Z")

</div>

In particular, we don't want parsing to in any way rely on semantics. In formal terms, we want the grammar to be Context-Free (modulo "mildly context sensitive" tokens, which iirc is just raw strings (and maybe nested block comments I forget the specifics off the top of my head)).

---

<div class="post-metadata">

**Author:** ![mcy](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mcy/32/6512_2.png) [@mcy](https://internals.rust-lang.org/u/mcy)\
**Post date:** [August 21, 2020, 6:21pm UTC](https://internals.rust-lang.org/t/yet-another-named-arguments-prototype/12937/6 "2020-08-21T18:21:33Z")

</div>

I don't think this affects parsing. By the time we're in HIR and evaluating macros, we know that a macro is requesting its path:

```rust
macro_rules! {
  ($path:self_path) => {...}
}

use somewhere::else::baz;
foo::Bar::baz!(); // foo::Bar is not a module.
// Expanded as if it were:
::somewhere::else::baz!(foo::Bar);
actually::a::module::baz!(); // Expansion is as-is-written, even
                             // if `actually::a::module::baz`
                             // turns out to not be a macro.

```

This is about expansion and name-resolution after parsing. The production is still `$path ! ( $tokens )` at the grammar level. Whether these semantics are reasonable is anyone's guess, but parsing is still context-free. Of course, if name resolution and macro expansion are not sequenced in the way I think they are, this doesn't work, but I'm pretty sure they have to be so that qualified macros work at all.

---

<div class="post-metadata">

**Author:** ![bjorn3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bjorn3/32/2736_2.png) [@bjorn3](https://internals.rust-lang.org/u/bjorn3)\
**Post date:** [August 21, 2020, 7:30pm UTC](https://internals.rust-lang.org/t/yet-another-named-arguments-prototype/12937/7 "2020-08-21T19:30:16Z")

</div>

Macros are expanded before the desugaring to hir. Instead it happens before the full name resolution. Instead a name resolution is used that only considers macros.

---

<div class="post-metadata">

**Author:** ![illicitonion](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/illicitonion/32/3886_2.png) [@illicitonion](https://internals.rust-lang.org/u/illicitonion)\
**Post date:** [August 21, 2020, 9:00pm UTC](https://internals.rust-lang.org/t/yet-another-named-arguments-prototype/12937/8 "2020-08-21T21:00:48Z")

</div>

> [@samsieber](#):
>
> What type of macro do you generate?

It generates a `macro_rules` macro... Ideally it would generate a procedural function-like macro, but as far as I can tell these can _only_ be created in crates which are marked `proc-macro = true` which is very restrictive.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [November 19, 2020, 9:00pm UTC](https://internals.rust-lang.org/t/yet-another-named-arguments-prototype/12937/9 "2020-11-19T21:00:50Z")

</div>

Closed on November 19, 2020, 9:00pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
