<p>In my language, I intend to have infix methods. The utility of this syntax feature is to express methods with the semantics of taking two objects, probably of the same type, in semantically equivalent positions. The examples include dot and cross product for vectors, string concatenation, and so on. Conventional method invocation syntax puts the right operand in a more nested position, and function invocation places the function in a prefix position, so both of these approaches fare poorly in nested cases in terms of visually asserting semantics. My question is, what syntax options are used for infix methods in established languages?</p>
<p>I've already considered user-defined operators as a potential solution: instead of allowing for a special kind of methods, the language could allow its users to define new operators consisting of arbitrary punctuation symbols. However, I fear this could easily lead to confusion, especially in large code bases, as well as wouldn't convey the meaning of the operation easily.</p>
<p>Preferably, I'd like to avoid introducing conflicting or confusing cases into my grammar. However, I'm interested in all answers for the sake of completeness.</p>

<syntax><language-design>
What syntax could infix method calls use?
<h3>Kotlin-style</h3>
<p>Simply placing the name of the method inbetween two operands (<code>a dot b</code>).</p>
<p><sup>I'm giving this answer myself because I've already considered this approach, and it seems viable at first glance. I'm still interested in alternatives.</sup></p>

CC BY-SA 4.0
2023-05-29T20:55:23.107Z
2023-05-29T20:55:23.107Z
abel1502
CC BY-SA 4.0
2023-05-30T03:19:39.917Z
This was and is the APL-style too, used by APL and its derivatives long before Kotlin's *parents*.
Adám
CC BY-SA 4.0
2023-05-30T17:45:43.807Z
@Adám note that Kotlin's parent does not use this (or do you mean the people who designed Kotlin?)
Seggan
CC BY-SA 4.0
2023-05-30T17:50:04.897Z
@Seggan Right, I didn't mean to imply that, only give some perspective on the time scales.
Adám
<h2>Smalltalk methods</h2>
<p>Smalltalk and Objective-C use mixfix syntax for multi-argument methods, e.g. <code>foo doSomethingWithFirstArg:1 secondArg:2 thirdArg:3</code> where C++ would have <code>foo.doSomething(1, 2, 3)</code>. Each part of the method name must end in a colon, and in Objective-C the entire expression must be wrapped in square brackets.</p>
<h2>Swift custom operators</h2>
<p>Swift allows users to define custom operators, but places some restrictions on what characters may occur in them. The rules are:</p>
<ul>
<li>Operators must consist of only the ASCII characters <code>./=-+!*%&lt;&gt;&amp;|^?~</code>, or certain non-ASCII characters listed in <a href="https://docs.swift.org/swift-book/documentation/the-swift-programming-language/lexicalstructure#Operators" rel="noreferrer">the Lexical Structure chapter of <em>The Swift Programming Language</em></a>.</li>
<li>If an operator does not begin with a <code>.</code>, it may not contain one. <code>.*</code> is a legal operator, but <code>*.</code> isn't.</li>
<li>Postfix operators may not begin with <code>?</code> or <code>!</code>, though prefix and infix operators can.</li>
<li>The following sequences aren't allowed as custom operators, though may exist as a subsequence of a longer operator:
<ul>
<li><code>=</code>, <code>/*</code>, <code>*/</code>, <code>//</code>, <code>-&gt;</code>, <code>.</code>, and <code>?</code></li>
<li><code>&lt;</code> and <code>&amp;</code> as prefix operators (fine as infix or postfix operators)</li>
<li><code>&gt;</code> and <code>!</code> as postfix operators (fine as prefix or infix operators)</li>
</ul>
</li>
</ul>

CC BY-SA 4.0
2023-05-29T22:24:57.580Z
2023-05-29T22:24:57.580Z
Bbrk24
<p><a href="https://wiki.haskell.org/Infix_operator" rel="noreferrer">Haskell's operators are just functions</a> with non-alphanumeric names and called in infix position.<br />
Alternatively, you can wrap an operator in parenthesis and use it as an identifier in prefix position, and you can wrap a non-operator function name in backticks and use in infix position.</p>
<p>Eg. <code>x + y</code> is the same as <code>(+) x y</code>, and <code>f x y</code> is the same as <code>x `f` y</code>.</p>
<p><a href="https://antelang.org/docs/language/#pipeline-operators" rel="noreferrer">Ante uses the dot <code>.</code> operator for that</a>, where <code>x .f y</code> is the same as <code>f x y</code> (it calls it the OO-friendly dot operator).<br />
That said, in the presence of currying, you do not actually need builtin support for infix non-operator function calls! Such a <code>.</code> operator can be defined as simply as</p>
<pre><code>(.): a -&gt; (a -&gt; b) -&gt; b
x . f = f x
</code></pre>
<p>which, with the same precedence as the function call, can be used as</p>
<pre><code>double x = x * 2
times = (*)

ten = 5 .double
answer = 10.5 .times 4
</code></pre>
<p>since for <code>ten</code>, <code>(.)</code> has type <code>Int -&gt; (Int -&gt; Int) -&gt; Int</code>, and for <code>answer</code>, the expression is interpreted as <code>(10.5 .times) 4</code>, and it has type <code>Rat -&gt; (Rat -&gt; (Int -&gt; Rat)) -&gt; (Int -&gt; Rat)</code>.</p>

CC BY-SA 4.0
2023-05-30T00:00:29.110Z
2023-05-30T00:27:48.563Z
2023-05-30T00:27:48.563Z
Longinus
# What syntax could infix method calls use?
Posted by: username_0 on 2023-05-29
In my language, I intend to have infix methods. The utility of this syntax feature is to express methods with the semantics of taking two objects, probably of the same type, in semantically equivalent positions. The examples include dot and cross product for vectors, string concatenation, and so on. Conventional method invocation syntax puts the right operand in a more nested position, and function invocation places the function in a prefix position, so both of these approaches fare poorly in nested cases in terms of visually asserting semantics. My question is, what syntax options are used for infix methods in established languages?

I've already considered user-defined operators as a potential solution: instead of allowing for a special kind of methods, the language could allow its users to define new operators consisting of arbitrary punctuation symbols. However, I fear this could easily lead to confusion, especially in large code bases, as well as wouldn't convey the meaning of the operation easily.

Preferably, I'd like to avoid introducing conflicting or confusing cases into my grammar. However, I'm interested in all answers for the sake of completeness.
**Tags:** syntax language-design
---
## 3 answers
### Answer by username_0 on 2023-05-29. Score: 6
### Kotlin-style

Simply placing the name of the method inbetween two operands (`a dot b`).

I'm giving this answer myself because I've already considered this approach, and it seems viable at first glance. I'm still interested in alternatives.
> **Comments:**
> **username_1** (2023-05-30): This was and is the APL-style too, used by APL and its derivatives long before Kotlin's *parents*.  
> **username_2** (2023-05-30): @Adám note that Kotlin's parent does not use this (or do you mean the people who designed Kotlin?)  
> **username_1** (2023-05-30): @Seggan Right, I didn't mean to imply that, only give some perspective on the time scales.  
---
### Answer by username_3 on 2023-05-29. Score: 5
## Smalltalk methods

Smalltalk and Objective-C use mixfix syntax for multi-argument methods, e.g. `foo doSomethingWithFirstArg:1 secondArg:2 thirdArg:3` where C++ would have `foo.doSomething(1, 2, 3)`. Each part of the method name must end in a colon, and in Objective-C the entire expression must be wrapped in square brackets.

## Swift custom operators

Swift allows users to define custom operators, but places some restrictions on what characters may occur in them. The rules are:

* Operators must consist of only the ASCII characters `./=-+!*%<>&|^?~`, or certain non-ASCII characters listed in [the Lexical Structure chapter of *The Swift Programming Language*](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/lexicalstructure#Operators).
* If an operator does not begin with a `.`, it may not contain one. `.*` is a legal operator, but `*.` isn't.
* Postfix operators may not begin with `?` or `!`, though prefix and infix operators can.
* The following sequences aren't allowed as custom operators, though may exist as a subsequence of a longer operator:
  + `=`, `/*`, `*/`, `//`, `->`, `.`, and `?`
  + `<` and `&` as prefix operators (fine as infix or postfix operators)
  + `>` and `!` as postfix operators (fine as prefix or infix operators)
---
### Answer by username_4 on 2023-05-30. Score: 5
Haskell's operators are just functions with non-alphanumeric names and called in infix position.
Alternatively, you can wrap an operator in parenthesis and use it as an identifier in prefix position, and you can wrap a non-operator function name in backticks and use in infix position.
Eg. x + y is the same as (+) x y, and f x y is the same as x `f` y.
Ante uses the dot . operator for that, where x .f y is the same as f x y (it calls it the OO-friendly dot operator).
That said, in the presence of currying, you do not actually need builtin support for infix non-operator function calls! Such a . operator can be defined as simply as
(.): a -> (a -> b) -> b
x . f = f x

which, with the same precedence as the function call, can be used as
double x = x * 2
times = (*)

ten = 5 .double
answer = 10.5 .times 4

since for ten, (.) has type Int -> (Int -> Int) -> Int, and for answer, the expression is interpreted as (10.5 .times) 4, and it has type Rat -> (Rat -> (Int -> Rat)) -> (Int -> Rat).
---
