# Overloading is evil, but

**URL:** <https://users.scala-lang.org/t/overloading-is-evil-but/12281>\
**Category:** Question\
**Created:** [May 27, 2026, 1:51pm UTC](https://users.scala-lang.org/t/overloading-is-evil-but/12281 "2026-05-27T13:51:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![charpov](https://users.scala-lang.org/user_avatar/users.scala-lang.org/charpov/32/500_2.png) [@charpov](https://users.scala-lang.org/u/charpov)\
**Post date:** [May 27, 2026, 1:51pm UTC](https://users.scala-lang.org/t/overloading-is-evil-but/12281/1 "2026-05-27T13:51:07Z")

</div>

This is a followup to my [Debugging transparent inline functions](https://users.scala-lang.org/t/debugging-transparent-inline-functions/12268) question as it turns out my problem has more to do with overloading than transparent inline.

This code works, and I don’t understand why:

```scala
def wf[A](code: ExecutionContext => A): Unit =
   code(ExecutionContext.global)

def wf[A](code: ExecutionContext => Future[A]): A =
   Await.result(code(ExecutionContext.global), Duration.Inf)

wf(e => Future("X")(using e))
wf(_ => "Y")

```

Don’t both `wf` functions have the same signature, `Function1`, after type erasure?

Then, this _doesn’t_ work:

```scala
def wc[A](code: ExecutionContext ?=> A): Unit =
   code(using ExecutionContext.global)

def wc[A](code: ExecutionContext ?=> Future[A]): A =
   Await.result(code(using ExecutionContext.global), Duration.Inf)

wc(Future("X"))
wc("Y")

```

The `wc` functions have the same bytecode-level signatures as the `wf` functions and can be compiled. However, neither can be invoked:

```plaintext
None of the overloaded alternatives of method wc with types
 [A](code: (scala.concurrent.ExecutionContext) ?=> scala.concurrent.Future[A]): A
 [A](code: (scala.concurrent.ExecutionContext) ?=> A²): Unit
match arguments (scala.concurrent.ExecutionContext => scala.concurrent.Future[String])

Found: ("Y" : String)
Required: scala.concurrent.ExecutionContext => <?>

```

I understand that overloading is messy (I’m trying to quit…), but I’d still like to understand what’s going on here.

---

<div class="post-metadata">

**Author:** ![som-snytt](https://users.scala-lang.org/letter_avatar_proxy/v4/letter/s/7cd45c/32.png) [@som-snytt](https://users.scala-lang.org/u/som-snytt)\
**Post date:** [May 27, 2026, 7:56pm UTC](https://users.scala-lang.org/t/overloading-is-evil-but/12281/2 "2026-05-27T19:56:08Z")

</div>

I don’t know if it’s a bug or limitation, but it works with explicit context function literal.

```scala
wc(ExecutionContext ?=> Future("X"))
wc(ExecutionContext ?=> "Y")

```

Maybe it has to do with the expected type computed for overloading.

The answer to the other question about why overloading with functions works is that the return type is part of the signature. If the return types are the same, you would resort to `@targetName`.

---

<div class="post-metadata">

**Author:** ![Ichoran](https://users.scala-lang.org/user_avatar/users.scala-lang.org/ichoran/32/550_2.png) [@Ichoran](https://users.scala-lang.org/u/Ichoran)\
**Post date:** [May 27, 2026, 7:59pm UTC](https://users.scala-lang.org/t/overloading-is-evil-but/12281/3 "2026-05-27T19:59:36Z")

</div>

I am not sure how to solve the problem, though I note that context functions evaluate the context parameter very eagerly, which suggests but does not enforce that they search less thoroughly than when the type is already known.

I offer this magic spell as my way of getting around this issue. However, although I love overloading normally, both the working lambda and the non-working context function make me nervous: there really isn’t much to distinguish these things.

Here is the incantation:

```auto
transparent inline def wc[A](code: ExecutionContext ?=> A): Any = summonFrom:
  case ev: (A <:< Future[b]) =>
    Await.result(ev(code(using ExecutionContext.global)), Duration.Inf)
  case _ =>
    code(using ExecutionContext.global)
    ()

```

---

<div class="post-metadata">

**Author:** ![charpov](https://users.scala-lang.org/user_avatar/users.scala-lang.org/charpov/32/500_2.png) [@charpov](https://users.scala-lang.org/u/charpov)\
**Post date:** [June 17, 2026, 4:41pm UTC](https://users.scala-lang.org/t/overloading-is-evil-but/12281/4 "2026-06-17T16:41:24Z")

</div>

> [@som-snytt](#):
>
> The answer to the other question about why overloading with functions works is that the return type is part of the signature.

This puzzled me because I’ve been teaching students otherwise for more years than I care to admit.

Java rejects this, even though the functions differ in their return types:

```java
void f(List<String> x) { }
int f(List<Integer> x) { return x.getFirst(); }

```

Scala _does_ accept this:

```scala
def f(x: List[String]): Unit = ()
def f(x: List[Int]): Int = x.head

```

However, this is rejected:

```scala
def f(x: List[Int]): Unit = ()
def f(x: List[Int]): Int = x.head

```

because it wouldn’t make much sense to accept it, and so is this:

```scala
def f(x: List[String]): Unit = ()
def f(x: List[Int]): Unit = x.head

```

presumably because these would be indistinguishable at the bytecode level (short of using `@targetName`).

I suppose it means that it’s a little more subtle that “the return type is (or is not) part of the signature” and that I need to be careful to distinguish between Java, Scala, and JVM bytecode in my understanding (and teaching) of overloading…

---

<div class="post-metadata">

**Author:** ![som-snytt](https://users.scala-lang.org/letter_avatar_proxy/v4/letter/s/7cd45c/32.png) [@som-snytt](https://users.scala-lang.org/u/som-snytt)\
**Post date:** [June 19, 2026, 5:25am UTC](https://users.scala-lang.org/t/overloading-is-evil-but/12281/5 "2026-06-19T05:25:12Z")

</div>

> [@charpov](#):
>
> I need to be careful to distinguish between Java, Scala, and JVM bytecode

Indeed.
