Skip to content

Don't use forwarders in closure implementation #4446

Closed
@allanrenucci

Description

@allanrenucci
class Foo {
  (x: Int) => x + 1
}

A closure that is not a SAM will desugar to something like this in Dotty:

class Foo {
  def apply(x: Int) = x + 1

  class $anon extends Function1[Int, Int] {
    def apply(x: Int) = Foo.this.apply(x)
  }
  new $anon
}

and this in scalac:

class Foo {
  class $anon extends Function1[Int, Int] {
    def apply(x: Int) =  x + 1
  }
  new $anon
}

The issue with the Dotty desugaring, is that $anon will capture a reference to the outer class Foo that is not needed

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions