Uploaded image for project: 'Scala Programming Language'
  1. Scala Programming Language
  2. SI-5543

Using this as default argument to constructor does not refer to enclosing object

    Details

      Description

      In the following code, the default argument to Bar is object Bar instead of object Main.

      However, scalac requires that both Main and Bar conform to the parameter type.

      I'd expect Main.this to be in scope, even though the expression is evaluated by Bar$.

      Reproduce Code

      package trythis
       
      //object Main { // type mismatch
      object Main extends Function0[Int] {
       
        class Bar(x: Function0[Int] = this) {
          override def toString = "X "+ x.toString +"="+ x()
        }
        object Bar extends Function0[Int] {
          def apply() = 21
          override def toString = "My Bar module"
        }
        def main(args: Array[String]): Unit = {
          println(new Bar())
        }
        override def toString = "My Main"
        def apply() = 17
      }
      

      Expected result

      X My Main=17
      

      Actual Result

      X My Bar module=21
      

        Attachments

          Activity

            People

            • Assignee:
              apm A. P. Marki
              Reporter:
              apm A. P. Marki
            • Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: