Skip to content

Collective extension syntax cannot be used inside function #8181

Closed
@SrTobi

Description

@SrTobi

minimized code

object Main {
  def main(args: Array[String]): Unit = {
    extension on (a: AnyRef) {
      def putout(): Unit = println(a)
    }
    
    "blub".putout()
  }
}

(usign 0.22.0-bin-20200202-c2478a3-NIGHTLY)

Compilation output

[error] -- [E006] Unbound Identifier Error: /home/tobi/workspace/playground/dotty-test/src/main/scala/Main.scala:4:4 
[error] 4 |    extension on (a: AnyRef) {
[error]   |    ^^^^^^^^^
[error]   |    Not found: extension
[error] -- [E008] Member Not Found Error: /home/tobi/workspace/playground/dotty-test/src/main/scala/Main.scala:8:11 
[error] 8 |    "blub".putout()
[error]   |    ^^^^^^^^^^^^^
[error]   |    value putout is not a member of String
[error] two errors found
[

expectation

Compiles

Note

Putting the extension clause outside it works:

object Main {
  extension on (a: AnyRef) {
    def putout(): Unit = println(a)
  }

  def main(args: Array[String]): Unit =
    "blub".putout()
}

and the desugared version (described here https://dotty.epfl.ch/docs/reference/contextual/extension-methods-new.html) also works:

object Main {
  def main(args: Array[String]): Unit = {
    given extension_ops: AnyRef {
      def (a: AnyRef).putout(): Unit = println(a)
    }

    "blub".putout()
  }
}

so this seems to be either a bug or an unnecessary restriction.

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