Skip to content

Compile error with functionName _ on Scala 2.12.0 #2656

@adamvoss

Description

@adamvoss

A method or function name followed by underscore does not appear be assignable to js.Function0 on Scala 2.12.0 while it is on Scala 2.11.8.

On Scala 2.11.8, the following code compiles and works:

package object mypackage {
  import scala.scalajs.js
  def render() = {}
  val handler: js.Function0[Any] = render _
}

However, on Scala 2.12.0, it gives a compilation error:

[error] /home/user/dev/untitled/src/main/scala/mypackage/Main.scala:4: type mismatch;
[error]  found   : Unit
[error]  required: scala.scalajs.js.Function0[Any]
[error]       setInterval(render _)
[error]                   ^

I am not sure if this is a known impact of the newer compiler, I couldn't figure out the right things to search for to find another record of this. It can be worked around, but is a little bit of a wart because if the signature of handler could be scala.Function0[Any], instead of js.Function0[Any], it would compile.

Workaround

Using lambda syntax rather than partial application syntax allows it to work. (Do I have that terminology right?)

Use

val handler: js.Function0[Any] = () => render()

instead of

val handler: js.Function0[Any] = render _

Investigation

Looking with -Xprint:typer it appears that on 2.12.0 render _ gets processed to be `package`.this.render() while () => render() ends up as (() => `package`.this.render()).

On 2.11.8 they are processed to js.this.Any.fromFunction0[Unit]({(() => `package`.this.render())} and js.this.Any.fromFunction0[Unit]((() => `package`.this.render()), respectively.

Scala.js 0.6.13

Metadata

Metadata

Assignees

No one assigned

    Labels

    separate repoAddressing this issue should be done in a separate repository. Closed here.upstreamThis issue derives from a bug or a change in one of our upstream dependencies.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions