# Infix notation for macros?

**URL:** <https://internals.rust-lang.org/t/infix-notation-for-macros/17406>\
**Category:** language design\
**Created:** [September 18, 2022, 8:46pm UTC](https://internals.rust-lang.org/t/infix-notation-for-macros/17406 "2022-09-18T20:46:49Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![axos88](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/axos88/32/5927_2.png) [@axos88](https://internals.rust-lang.org/u/axos88)\
**Post date:** [September 18, 2022, 8:46pm UTC](https://internals.rust-lang.org/t/infix-notation-for-macros/17406/1 "2022-09-18T20:46:50Z")

</div>

To illustrate with an example, I would love to see the ability to write something like:

```rust
  let r = some_operation_returning_a_result().expect_format!("An unknown error occured doing something: {:?}")

```

Which would roughly transform into:

```rust
  let r = some_operation_returning_a_result().map_err( |e| panic!("An unknown error occured doing something: {:?}", e))?

```

Note the infix notation for the macro expect\_format!, whose input would be the expression it was called on. This notation would be more fluently readable than what can be done currently:

```rust
  expect_format!(some_operation_returning_a_result(), "An unknown error occured doing something: {:?}");

```

---

<div class="post-metadata">

**Author:** ![Jules-Bertholet](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jules-bertholet/32/10671_2.png) [@Jules-Bertholet](https://internals.rust-lang.org/u/Jules-Bertholet)\
**Post date:** [September 18, 2022, 8:50pm UTC](https://internals.rust-lang.org/t/infix-notation-for-macros/17406/2 "2022-09-18T20:50:49Z")

</div>

There is an open RFC for this: [https://github.com/rust-lang/rfcs/pull/2442](https://github.com/rust-lang/rfcs/pull/2442)

---

<div class="post-metadata">

**Author:** ![afetisov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/afetisov/32/8508_2.png) [@afetisov](https://internals.rust-lang.org/u/afetisov)\
**Post date:** [September 19, 2022, 11:02am UTC](https://internals.rust-lang.org/t/infix-notation-for-macros/17406/3 "2022-09-19T11:02:11Z")

</div>

Terminological note: that's not "infix", that's "postfix", because it acts as a postfix operator with a unique left-to-right precedence. Infix macros would mean something like

```rust
let r = some_operation_returning_a_result() expect_format! "An unknown error occured doing something: {:?}";

```

Note the absence of dot operator and the brackets, which mean that now you would also need to deal with precedence of various infix macros, as well as other infix operators (`+, -, *, /, %`). That basically moves into the "arbitrary operator symbols" territory, which Rust prefers to avoid.

---

<div class="post-metadata">

**Author:** ![marcianx](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/marcianx/32/3249_2.png) [@marcianx](https://internals.rust-lang.org/u/marcianx)\
**Post date:** [September 19, 2022, 12:26pm UTC](https://internals.rust-lang.org/t/infix-notation-for-macros/17406/4 "2022-09-19T12:26:19Z")

</div>

How is it postfix if it comes between two of its arguments ("receiver" and the rest)? Wouldn't postfix means that it comes after both like RPN?

---

<div class="post-metadata">

**Author:** ![SkiFire13](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/skifire13/32/7579_2.png) [@SkiFire13](https://internals.rust-lang.org/u/SkiFire13)\
**Post date:** [September 19, 2022, 4:51pm UTC](https://internals.rust-lang.org/t/infix-notation-for-macros/17406/5 "2022-09-19T16:51:57Z")

</div>

I would argue this is neither infix nor postfix because it is not represented as a single symbol. The final parenthesis is part of the operator but it's after the second operand, separated from the rest of the operator.

---

<div class="post-metadata">

**Author:** ![zackw](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/zackw/32/2071_2.png) [@zackw](https://internals.rust-lang.org/u/zackw)\
**Post date:** [September 19, 2022, 5:07pm UTC](https://internals.rust-lang.org/t/infix-notation-for-macros/17406/6 "2022-09-19T17:07:15Z")

</div>

The function call operator is an odd duck. I can think of at least three different ways to model it, but for purposes of macros in method-call position, I think it makes sense to call it "postfix" because it's postfix _relative to the method receiver_ and that may (or may not, depending on how we decide method-like macro dispatch works -- I have no opinion there) affect which macro is being invoked.

The arguments (if any) within the parentheses don't affect dispatch (at least, I haven't seen anyone proposing that for Rust) and you _could_ describe funcall as "merely" passing them through to the callee, rather than actually _operating_ on them.

---

<div class="post-metadata">

**Author:** ![jdahlstrom](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jdahlstrom/32/3351_2.png) [@jdahlstrom](https://internals.rust-lang.org/u/jdahlstrom)\
**Post date:** [September 20, 2022, 10:42am UTC](https://internals.rust-lang.org/t/infix-notation-for-macros/17406/7 "2022-09-20T10:42:46Z")

</div>

> [@zackw](#):
>
> The arguments (if any) within the parentheses don't affect dispatch (at least, I haven't seen anyone proposing that for Rust) and you _could_ describe funcall as "merely" passing them through to the callee, rather than actually _operating_ on them.

~~To be clear, they don't (wouldn't) affect _macro_ dispatch (insofar as the pattern matching within a macro is not called "dispatch", anyway). They definitely affect _method_ dispatch because Rust has a form of method overloading based on trait impls.~~ Edit: oops, ignore this.

---

<div class="post-metadata">

**Author:** ![Nemo157](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nemo157/32/11585_2.png) [@Nemo157](https://internals.rust-lang.org/u/Nemo157)\
**Post date:** [September 20, 2022, 11:55am UTC](https://internals.rust-lang.org/t/infix-notation-for-macros/17406/8 "2022-09-20T11:55:57Z")

</div>

Method dispatch [does not consider the types of the (non-Self) arguments](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=867495ec2a71ed3f71be9a84a9237a12)

```nohighlight
error[E0034]: multiple applicable items in scope
8 | ().foo(5i32);
  | ^^^ multiple `foo` found
note: candidate #1 is defined in an impl of the trait `Foo1` for the type `()`
1 | trait Foo1 { fn foo(&self, arg: i32) { } }
note: candidate #2 is defined in an impl of the trait `Foo2` for the type `()`
4 | trait Foo2 { fn foo(&self, arg: &str) { } }

```

(EDIT: You _can_ implement method overloads by having an extra dispatching function that pushes all types into the `Self` type through an extra trait though, [quick overcomplicated playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=0fa20362af0cb5af6813977daf7b3b8e), real usecases can normally be done much simpler).

---

<div class="post-metadata">

**Author:** ![ckaran](https://avatars.discourse-cdn.com/v4/letter/c/f475e1/32.png) [@ckaran](https://internals.rust-lang.org/u/ckaran)\
**Post date:** [September 20, 2022, 2:27pm UTC](https://internals.rust-lang.org/t/infix-notation-for-macros/17406/9 "2022-09-20T14:27:58Z")

</div>

If it weren't for the fact that it's a bad idea from a readability point of view (and because the parser [chokes on at least some unicode characters](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=5b9188959399ea0b5cfeb5f68b44d452)), you could _almost_ use this for infix math operations:

```rust
a.⋐!(b)

```

---

<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:** [September 21, 2022, 7:15pm UTC](https://internals.rust-lang.org/t/infix-notation-for-macros/17406/10 "2022-09-21T19:15:29Z")

</div>

> [@ckaran](#):
>
> and because the parser [chokes on at least some unicode characters](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=5b9188959399ea0b5cfeb5f68b44d452)

Rust uses the _standard_ definition of what characters are allowed in identifiers, [UAX#31 Unicode Identifier and Pattern Syntax](https://unicode.org/reports/tr31/) (specifically, [UAX31-R1-2](https://unicode.org/reports/tr31/#R1-2), although I don't recall the exact profile atm; it's the obvious one to adjust the ASCII character set).

I actually do agree in principle that mathematical symbols should be considered in UAX31 (probably by a new property like Pattern\_Syntax but extensible like the XID sets), but that's a question for the Unicode Consortium to hash out. Rust rightly just defers to Unicode's definition of what a source code identifier should look like rather than making its own definition.

---

<div class="post-metadata">

**Author:** ![ckaran](https://avatars.discourse-cdn.com/v4/letter/c/f475e1/32.png) [@ckaran](https://internals.rust-lang.org/u/ckaran)\
**Post date:** [September 21, 2022, 8:58pm UTC](https://internals.rust-lang.org/t/infix-notation-for-macros/17406/11 "2022-09-21T20:58:56Z")

</div>

Thank you, I didn't know that Unicode had a standard for identifiers. And as much I hate it, I think you're right that Rust needs to stick to the standard here.

---

<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:** [December 20, 2022, 8:59pm UTC](https://internals.rust-lang.org/t/infix-notation-for-macros/17406/12 "2022-12-20T20:59:29Z")

</div>

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