Skip to content

SAM convert not work in overloaded method parameter. #12560

@counter2015

Description

@counter2015

Reproduction steps

Scala Version: 2.13.8

I cant reproduce it by raw scala, it's only reproduce in akka function.

I'm not sure if the bug is also related to akka.

Add akka library in sbt

libraryDependencies += "com.typesafe.akka" %% "akka-actor" % "2.5.32"

And then compile following code

import akka.actor.ActorSystem

import scala.concurrent.duration._

object BugExample {

  def main(args: Array[String]): Unit = {
    val system = ActorSystem("test")
    implicit val ec = system.dispatcher

    system.scheduler.schedule(1.second, 10.seconds, () => println("why cant convert this to Runnable"))
  }
}

And here is the schedule method

  def schedule(initialDelay: FiniteDuration, interval: FiniteDuration, runnable: Runnable)(
      implicit executor: ExecutionContext): Cancellable

Compile failed with error

BugExample.scala:11:56: type mismatch;
[error]  found   : () => Unit
[error]  required: Runnable
[error]         system.scheduler.schedule(1.second, 10.seconds, () => println("why cant convert this to Runnable"))

Problem

Following code works well

import akka.actor.ActorSystem

import scala.concurrent.ExecutionContext
import scala.concurrent.duration._

object BugExample {

  // self defined function works well
  def test1() = {
    def schedule(initialDelay: FiniteDuration, interval: FiniteDuration, runnable: Runnable)(
      implicit executor: ExecutionContext): Unit = {
      println(s"$initialDelay $interval")
      runnable.run()
    }

    val system = ActorSystem("test")
    implicit val ec = system.dispatcher

    schedule(1.second, 10.seconds, () => println("works well 1"))
  }

  // declare type explicitly works well
  def test2() = {


    val system = ActorSystem("test")
    implicit val ec = system.dispatcher

    val f: Runnable = () => println("works well 2")

    system.scheduler.schedule(1.second, 10.seconds, f)
  }


  def main(args: Array[String]): Unit = {
    test1()
    test2()
  }

}

IMO, the compiler should convert () => scala.Unit to Runnable .
What's the problem here ?


Environment:

sbt: 1.6.2
jdk: corretto 17, openjdk 11

Metadata

Metadata

Assignees

Labels

fixed in Scala 3This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/)has PRoverloadingsam

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions