# Justification for Rust not Supporting Function Overloading (directly)

**URL:** <https://internals.rust-lang.org/t/justification-for-rust-not-supporting-function-overloading-directly/7012>\
**Category:** Uncategorized\
**Created:** [March 10, 2018, 7:46pm UTC](https://internals.rust-lang.org/t/justification-for-rust-not-supporting-function-overloading-directly/7012 "2018-03-10T19:46:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gbutler](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/gbutler/32/3670_2.png) [@gbutler](https://internals.rust-lang.org/u/gbutler)\
**Post date:** [March 10, 2018, 7:46pm UTC](https://internals.rust-lang.org/t/justification-for-rust-not-supporting-function-overloading-directly/7012/1 "2018-03-10T19:46:03Z")

</div>

There was recently a [Reddit post](https://www.reddit.com/r/rust/comments/83g847/exploring_function_overloading/) regarding implementing function overloading through “clever” use of Traits. My personal feeling is that it was demonstrating a Rust anti-pattern, in particular, as well as a general anti-pattern. I personally find the lack of Function Overloading in Rust to be the correct choice and think “teaching” how to emulate design patterns that Rust specifically has decide aren’t warranted is not a good idea.

I suggested the original poster add some comment regarding the Rust choices and perhaps some links to discussions regarding the decision in Rust. Little did I know, such discussions aren’t easily found. I couldn’t find anything directly relevant in the current RFC’s (at least by searching) and nothing relevant is showing up through Google-Fu. My guess is these discussions were solidified before the current RFC process was adopted.

Does anyone have any links to discussions, RFC’s, documents that provides reasoning about why this choice was made in Rust. It would be nice to have something like the above referenced Reddit post, and the Blog post it references, reference such discussions and/or justifications.

---

<div class="post-metadata">

**Author:** ![jesselucas](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jesselucas/32/3884_2.png) [@jesselucas](https://internals.rust-lang.org/u/jesselucas)\
**Post date:** [March 10, 2018, 9:10pm UTC](https://internals.rust-lang.org/t/justification-for-rust-not-supporting-function-overloading-directly/7012/2 "2018-03-10T21:10:06Z")

</div>

I found this blog post: [Abstraction without overhead: traits in Rust | Rust Blog](https://blog.rust-lang.org/2015/05/11/traits.html)

> Overloading. Rust does not support traditional overloading where the same method is defined with multiple signatures. But traits provide much of the benefit of overloading: if a method is defined generically over a trait, it can be called with any type implementing that trait. Compared to traditional overloading, this has two advantages. First, it means the overloading is less ad hoc: once you understand a trait, you immediately understand the overloading pattern of any APIs using it. Second, it is extensible: you can effectively provide new overloads downstream from a method by providing new trait implementations.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [March 10, 2018, 11:19pm UTC](https://internals.rust-lang.org/t/justification-for-rust-not-supporting-function-overloading-directly/7012/3 "2018-03-10T23:19:28Z")

</div>

There probably isn’t much about it explicitly, because it’s a direct consequence of a different design choice: the desire to not have monomorphization-time errors in generics.

Take a function like this:

```rust
fn foo<T>(x: T) { bar(x) }

```

Today, that’s easy:

- the call to `bar` is to the `bar` that’s in-scope at the definition of `foo`
- `bar` must be a function that accepts anything, since `T` is unbounded

Imagine overloading existed. Both would be way harder:

- Which `bar` are we calling? Is it the `bar` in-scope right now? If there are two in-scope right now, which of them? An overload of `bar` that doesn’t exist yet? A `bar` [in the same module where the concrete generic argument is defined](https://en.wikipedia.org/wiki/Argument-dependent_name_lookup)?
- When is `foo` even valid? Is it “whenever there’s a `bar` somewhere that happens to match what we need”? Do we need to like all the types for which `bar` exists in the bound somewhere?

Explicitly indirecting through the trait, rather than having everything be a potential indirection point, makes everything was easier, both to understand what’s happening and to implement.

---

<div class="post-metadata">

**Author:** ![gbutler](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/gbutler/32/3670_2.png) [@gbutler](https://internals.rust-lang.org/u/gbutler)\
**Post date:** [March 10, 2018, 11:36pm UTC](https://internals.rust-lang.org/t/justification-for-rust-not-supporting-function-overloading-directly/7012/4 "2018-03-10T23:36:50Z")

</div>

So, are you saying that “Easy Monomorphization” was deemed more important and useful than “Non-Generic, Variable-Arity Function Overloading”? I would agree with that conclusion, but, it would be nice to point back to some design discussion early in the language where the pros and cons were hashed out and the decisions made.

---

<div class="post-metadata">

**Author:** ![gbutler](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/gbutler/32/3670_2.png) [@gbutler](https://internals.rust-lang.org/u/gbutler)\
**Post date:** [March 11, 2018, 12:43am UTC](https://internals.rust-lang.org/t/justification-for-rust-not-supporting-function-overloading-directly/7012/5 "2018-03-11T00:43:01Z")

</div>

Thanks for this. Pretty much exactly what I was looking for.

---

<div class="post-metadata">

**Author:** ![steveklabnik](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steveklabnik/32/4524_2.png) [@steveklabnik](https://internals.rust-lang.org/u/steveklabnik)\
**Post date:** [March 11, 2018, 1:05pm UTC](https://internals.rust-lang.org/t/justification-for-rust-not-supporting-function-overloading-directly/7012/6 "2018-03-11T13:05:33Z")

</div>

I think the framing is slightly off. I’m not aware of Rust ever having function overloading, so the question is not “why does Rust not support function overloading” but “what would allow Rust to support function overloading?” Features only get added if there’s a proposal, with justifications. I can’t remember, but I’m pretty sure there was never a proposal, and so therefore, no rejection of a proposal either.

---

<div class="post-metadata">

**Author:** ![leodasvacas](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/leodasvacas/32/4060_2.png) [@leodasvacas](https://internals.rust-lang.org/u/leodasvacas)\
**Post date:** [March 11, 2018, 1:41pm UTC](https://internals.rust-lang.org/t/justification-for-rust-not-supporting-function-overloading-directly/7012/7 "2018-03-11T13:41:48Z")

</div>

tldr: Generics and function overloading don't play along well. Quoting boats [here](https://users.rust-lang.org/t/is-it-possible-to-specialize-hashmap-index-for-copy-types/7750/5?u=leodasvacas):

> [@On another topic](https://internals.rust-lang.org/t/7750/5):
>
> The feature your asking for is just plain old function overloading, which Rust does not have. There are a few reasons I think its unlikely Rust will have this feature any time soon:
> 
> 1. It’s not usually that useful, since you can just use a different method name.
> 
> 2. Using it is often not a great idea, because if these methods have different signatures and different implementations, like as not they should have a different name.
> 
> 3. In a language with a lot of type parameters flying around like Rust has, proving that the two signatures are actually disjoint is not trivial. We’d have to have a whole coherence system for function signatures, and before long we’d be talking about specialization between signatures.

---

<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:** [March 25, 2019, 8:29am UTC](https://internals.rust-lang.org/t/justification-for-rust-not-supporting-function-overloading-directly/7012/8 "2019-03-25T08:29:52Z")

</div>

Closed on March 25, 2019, 8:29am UTC

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