# Matching on a context function

**URL:** <https://users.scala-lang.org/t/matching-on-a-context-function/10562>\
**Category:** Question\
**Created:** [February 7, 2025, 7:38pm UTC](https://users.scala-lang.org/t/matching-on-a-context-function/10562 "2025-02-07T19:38:40Z")\
**Posts on this page:** 7\
**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:** [February 7, 2025, 7:38pm UTC](https://users.scala-lang.org/t/matching-on-a-context-function/10562/1 "2025-02-07T19:38:40Z")

</div>

This code is in the spirit of what I need to do:

```scala
transparent inline def eval[A, B](f: A => B, x: A): Any =
   inline f match
      case g: (A => Future[?]) => g(x).value.get.get
      case _ => f(x)

eval((x: Int) => Future.successful(x), 10)
eval((x: Int) => x + 1, 10)    

```

Both expressions have type `Int`, which is what I want. In my actual case, however, `f` has type `A ?=> B` instead of `A => B`. Can I still match on `f` to branch at compile-time depending on the type `B`?

[https://scastie.scala-lang.org/IkmU8HNHQk2zmLrEXEqnRQ](https://scastie.scala-lang.org/IkmU8HNHQk2zmLrEXEqnRQ)

---

<div class="post-metadata">

**Author:** ![Jasper-M](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jasper-m/32/872_2.png) [@Jasper-M](https://users.scala-lang.org/u/Jasper-M)\
**Post date:** [February 10, 2025, 1:22pm UTC](https://users.scala-lang.org/t/matching-on-a-context-function/10562/2 "2025-02-10T13:22:42Z")

</div>

The main issue seems to be that there’s not really a good generally applicable way to just use `f` in an expression when `f` has type `A ?=> B` _without_ meaning to implicitly invoke `f`.

But this workaround worked for me:

```scala
transparent inline def eval[A, B](f: A ?=> B, x: A): Any =
   val f0 = a => f(using a)
   inline f0 match
      case g: (A => Future[?]) => g(x).value.get.get
      case _ => f0(x)

eval(Future.successful(summon[Int]), 10)
eval(summon[Int] + 1, 10)

```

---

<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:** [February 10, 2025, 2:41pm UTC](https://users.scala-lang.org/t/matching-on-a-context-function/10562/3 "2025-02-10T14:41:39Z")

</div>

Excellent! That should work. I’ll probably need intermediate functions for what I want to do, and those can be in terms of `A => B` as long as `A ?=> B` stays in the user-level interface. Thanks a lot.

---

<div class="post-metadata">

**Author:** ![Jasper-M](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jasper-m/32/872_2.png) [@Jasper-M](https://users.scala-lang.org/u/Jasper-M)\
**Post date:** [February 10, 2025, 2:44pm UTC](https://users.scala-lang.org/t/matching-on-a-context-function/10562/4 "2025-02-10T14:44:54Z")

</div>

Yeah I thought at least `inline (f: A ?=> B) match` should work but even then the compiler complains about not finding an implicit `A`. That might actually be a bug, given that `val g: A ?=> B = f` does work.

---

<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:** [February 12, 2025, 8:45pm UTC](https://users.scala-lang.org/t/matching-on-a-context-function/10562/5 "2025-02-12T20:45:01Z")

</div>

I was able to accomplish what I wanted, but I’m getting a spurious warning on `f0` being unused. Unfortunately, I’ve not been able to reproduce it on Scastie. It seems to be triggered by compiling one of my tests, as if something has become unused _after_ inlining, but I can’t figure out where exactly.

---

<div class="post-metadata">

**Author:** ![Jasper-M](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jasper-m/32/872_2.png) [@Jasper-M](https://users.scala-lang.org/u/Jasper-M)\
**Post date:** [February 13, 2025, 9:44am UTC](https://users.scala-lang.org/t/matching-on-a-context-function/10562/6 "2025-02-13T09:44:30Z")

</div>

Is it a real compiler warning or something from Intellij? Kinda sounds like a bug anyway.

---

<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:** [February 13, 2025, 1:55pm UTC](https://users.scala-lang.org/t/matching-on-a-context-function/10562/7 "2025-02-13T13:55:38Z")

</div>

It’s a real warning (using `-Wunused:all`). I don’t know the fancy compiler options that would let me investigate further, e.g., to find the specific test that triggers the warning. With it, I might be able to recreate the issue for a bug report.
