Skip to content

Placeholder syntax for anonymous functions chooses too narrow a scope for _: T #903

Closed
@retronym

Description

@retronym

This program compiles with scalac:

object Test {
  def contains(s: String, i: Int) = true
  def test1 = {
    val f = contains("", (_: Int)) // dotc:
    f.apply(0)
    //     sandbox/eta.scala:4: error: type mismatch:
    //  found   : Int => Int
    //  required: Int
    //     val f = contains("", (_: Int))
    //                          ^
    // sandbox/eta.scala:5: error: apply is not a member of Boolean(f)
    //     f.apply(0)
    //      ^
  }

  def test2 = {
    val f = "".contains("", (_: Int)) // dotc:
    f.apply(0)
    // sandbox/eta.scala:18: error: apply is not a member of Boolean(f)
    //     f.apply(0)
    //       ^
  }
}

But with dotty, if tails with the compile errors inlined in comments above. Here's the post-typer AST:

package <empty> {
  final lazy module val Test: Test$ = new Test$()
  final module class Test$() extends Object() { this: Test.type =>
    def contains(s: String, i: Int): Boolean = true
    def test1: <error> = {
      val f: Boolean =
        Test.contains("", {
          def $anonfun(_$1: Int): Int = _$1: Int
          closure($anonfun)
        })
      f.apply(0)
    }
    def test2: <error> = {
      val f: Boolean =
        augmentString("").contains[Any'](
          Tuple2.apply[String', (Int => Int)']("", {
            def $anonfun(_$2: Int): Int = _$2: Int
            closure($anonfun)
          })
        )
      f.apply(0)
    }
  }
}

Tested with edfb6e7

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