<p>Extension methods and Uniform Function Call Syntax (UFCS) are two different ways of letting you write a method call like <code>foo.bar()</code> without <code>bar</code> being a method originally defined on <code>foo</code>.</p>
<p>Extension methods are methods added to a type outside of the type's original definition. They let you add a <code>.sum()</code> method on a type such as <code>List</code> even if you didn't define <code>List</code> yourself. This <code>.sum()</code> extension method, however, is not a free function that can be passed around. It must be called on a <code>List</code> object. C#, Kotlin, and many other languages have extension methods.</p>
<p>UFCS is very similar, except there's no special extension methods that are treated differently. You can take a free function that would normally be called like <code>foo(bar, baz)</code> and instead call it like <code>bar.foo(baz)</code>. Ideally, this would also work the other way: a method <code>.foo()</code> on some object <code>bar</code> should be able to be called as <code>foo(bar)</code> too (although D, a language with UFCS, doesn't fully support this).</p>
<p>My question is this: what are the advantages and disadvantages of extension methods and uniform function call syntax? What trade-offs are made when choosing one over the other, and in what circumstance would you prefer one over the other?</p>

<syntax><language-design>
Advantages of extension methods vs uniform function call syntax (UFCS)?
<p>The most important difference is that extension methods do not require that the language has overloading, while UFCS is built on top of overloading.</p>

CC BY-SA 4.0
2023-05-17T07:57:24.127Z
2023-05-17T07:57:24.127Z
Nuoji
CC BY-SA 4.0
2023-07-20T23:44:02.460Z
This allowed Kotlin to use them to great effect to build their own collections library on top of Java collections classes that they cannot add methods to.
Mike Samuel
CC BY-SA 4.0
2023-07-27T22:31:32.343Z
This makes it sound as if UFCS could not be implemented in a dynamically typed language, but I really can't see a reason for that. It would just require some rules for when to treat `bar.foo` as an attribute lookup and when to treat it as a sugared call to `foo`.
Karl Knechtel
CC BY-SA 4.0
2023-08-24T01:33:20.643Z
@KarlKnechtel Overloading is a dynamically-typed language is just called "multimethods". Julia designs its entire semantics around multimethods, and it works quite well in practice.
Silvio Mayolo
CC BY-SA 4.0
2023-08-02T14:20:40.707Z
@KarlKnechtel consider the function `foo(bar, baz)`, then we must at least assume that this function is different from `foo(abc, baz)` i.e. there is a concept of overloading or the use is very limited as we cannot reuse any function name once it is defined.
Nuoji
<h1>New conformances</h1>
<p>In languages with nominal rather than structural typing, UFCS doesn't let you add a new protocol conformance to an existing type, but extensions can, e.g. (Swift):</p>
<pre><code>protocol MyNumericThing {
  func add(_ other: Self) -&gt; Self
}

extension Int: MyNumericThing {
  func add(_ other: Int) -&gt; Int { self + other }
}
</code></pre>
<p>Of course, this comes with the danger of <em>retroactive conformances</em>, which you can read more about <a href="https://forums.swift.org/t/retroactive-conformances-vs-swift-in-the-os/14393" rel="nofollow noreferrer">here</a>.</p>

CC BY-SA 4.0
2023-05-17T02:58:16.130Z
2023-05-17T03:03:19.143Z
2023-05-17T03:03:19.143Z
Bbrk24
<h2>Freedom in calling syntax</h2>
<p>Without UFCS, you have to call a procedure/method with the syntax that the author wanted you to use. With UFCS, you can freely decide which syntax works better in your context.</p>

CC BY-SA 4.0
2023-07-19T08:23:52.787Z
2023-07-19T08:23:52.787Z
xigoi
# Advantages of extension methods vs uniform function call syntax (UFCS)?
Posted by: username_0 on 2023-05-17
Extension methods and Uniform Function Call Syntax (UFCS) are two different ways of letting you write a method call like `foo.bar()` without `bar` being a method originally defined on `foo`.

Extension methods are methods added to a type outside of the type's original definition. They let you add a `.sum()` method on a type such as `List` even if you didn't define `List` yourself. This `.sum()` extension method, however, is not a free function that can be passed around. It must be called on a `List` object. C#, Kotlin, and many other languages have extension methods.

UFCS is very similar, except there's no special extension methods that are treated differently. You can take a free function that would normally be called like `foo(bar, baz)` and instead call it like `bar.foo(baz)`. Ideally, this would also work the other way: a method `.foo()` on some object `bar` should be able to be called as `foo(bar)` too (although D, a language with UFCS, doesn't fully support this).

My question is this: what are the advantages and disadvantages of extension methods and uniform function call syntax? What trade-offs are made when choosing one over the other, and in what circumstance would you prefer one over the other?
**Tags:** syntax language-design
---
## 3 answers
### Answer by username_1 on 2023-05-17. Score: 7
The most important difference is that extension methods do not require that the language has overloading, while UFCS is built on top of overloading.
> **Comments:**
> **username_2** (2023-07-20): This allowed Kotlin to use them to great effect to build their own collections library on top of Java collections classes that they cannot add methods to.  
> **username_3** (2023-07-27): This makes it sound as if UFCS could not be implemented in a dynamically typed language, but I really can't see a reason for that. It would just require some rules for when to treat `bar.foo` as an attribute lookup and when to treat it as a sugared call to `foo`.  
> **username_1** (2023-08-02): @KarlKnechtel consider the function `foo(bar, baz)`, then we must at least assume that this function is different from `foo(abc, baz)` i.e. there is a concept of overloading or the use is very limited as we cannot reuse any function name once it is defined.  
> **username_4** (2023-08-24): @KarlKnechtel Overloading is a dynamically-typed language is just called "multimethods". Julia designs its entire semantics around multimethods, and it works quite well in practice.  
---
### Answer by username_5 on 2023-05-17. Score: 4
New conformances
In languages with nominal rather than structural typing, UFCS doesn't let you add a new protocol conformance to an existing type, but extensions can, e.g. (Swift):
protocol MyNumericThing {
  func add(_ other: Self) -> Self
}

extension Int: MyNumericThing {
  func add(_ other: Int) -> Int { self + other }
}

Of course, this comes with the danger of retroactive conformances, which you can read more about here.
---
### Answer by username_6 on 2023-07-19. Score: 3
## Freedom in calling syntax

Without UFCS, you have to call a procedure/method with the syntax that the author wanted you to use. With UFCS, you can freely decide which syntax works better in your context.
---
