SAM and PolyFunction

I was interested to see if dotty’s polyfunction could be used with SAM (single abstract method) instantiation and it seems like it can’t

scala> trait FunctionK[F[_], G[_]] {
     |   def apply[A](f: F[A]): G[A]
     | }
// defined trait FunctionK

scala> def run[F[_], G[_], A](f: F[A])(fn: FunctionK[F, G]): G[A] = fn(f)
def run[F[_$1], G[_$2], A](f: F[A])(fn: FunctionK[F, G]): G[A]

scala> run(List(1)) { [A] => (list: List[A]) => list.headOption }
1 |run(List(1)) { [A] => (list: List[A]) => list.headOption }
  |                                                        ^
  |Found:    Object with PolyFunction {...}
  |Required: FunctionK[F, G]
  |
  |where:    F is a type variable with constraint >: List and <: [_$1] =>> Any
  |          G is a type variable with constraint <: [_$2] =>> Any

I just wanted to say, this would be really nice to have since SAMs with parameters on the single abstract method are really not that unusual, especially in functional programming settings. It would be really nice if we the compiler could see these examples and instantiate a new anonymous instance at the call site.

Note, this example is a bit contrived, but consider the example of:

trait Semigroupal[F[_]] {
  def combine[A, B](fa: F[A], fb: F[B]): F[(A, B)]
}

it would be nice to be able to just do [A,B] => (fa: Option[A], fb: Option[B]) => fa.zip(fb) to make an instance (if Option’s instance wasn’t around for example).

9 Likes

Yup, it’s on my todo list.

9 Likes

Yup, it’s on my todo list.

I was wondering, with increasing Scala3 adoption is there any traction on this feature?

It’s still on my mind, but not something I’ll personally have time to work on before I’m done with my thesis this summer.

Hey @smarter, if someone let’s say wanted to help and work on this. Do you have some time to mentor and give some pointers/advise on where to start ?

By the way, thanks for the amazing work you guys do on Scala!

I’m afraid I don’t have much time right now. But some pointers:

1 Like

Hello,
I worked under smarter’s supervision on polymorphic eta-expansion
polymorphic SAM was among the “future work” of my project
So if you have any questions PM me, and I’ll try to answer as best I can !

2 Likes