Skip to content

Parameterless function identified as function with one Unit parameter #10437

Closed
@gaeljw

Description

@gaeljw

Minimized code

import scala.language.implicitConversions

final class MyClass(name: String) {

  final class Fun0(val f: Function0[Any])

  object Fun0 {

    implicit def function0AsFun0(f: Function0[Any]): Fun0 = new Fun0(f)

  }

  def apply(f: => Unit): Unit = {
    apply(() => f)
  }

  def apply(fun: Fun0): Unit = {
    // Do something
    println(s"Got a Fun0 $fun")
  }

  def apply[T1](f: (T1) => Any)(implicit m1: Manifest[T1]): Unit = {
    // Do something
    println(s"Got a Function1: ${f}")
  }

}

Output

12 |    apply(() => f)
   |                  ^
   |                  No Manifest available for Unit.

From what I understand the () => f is interpreted as Unit => Any rather than () => Any.

Expectation

This code compiles fine with Scala 2.13. Although I admit it's a bit twisted, I expected it to compile in Scala 3 as well.

Note that I found two workarounds:

// 1st workaround
def apply(f: => Unit): Unit = {
  apply((() => f)) // Notice the double parenthesis
}

// 2nd workaround
def apply(f: => Unit): Unit = {
  val fun = () => f
  apply(fun) 
}

This might be intended behavior and I can accept it easily but I prefer to post and ask in case this could be the indicator of a more serious issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions