Skip to content

problem with anonymous class while using self-type + structural type #11073

Open
@ibaklan

Description

@ibaklan

Problem occurred when I'm trying to instantiate following class (with self-type construct) by the means of anonymous class (new Foo {...})

  type F0 = {
    def apply() : Unit
  }

  class Foo {
    this: F0 =>

    def bar(): Unit = {
      println("call apply")
      apply()
    }
  }

Entire code snippet may look like this:
( fails with Error:(13, 19) illegal inheritance; self-type crafts.Main7.Foo does not conform to Main7.this.Foo's selftype crafts.Main7.Foo with crafts.Main7.F0 )

package crafts

object Main7 {

  def main(args: Array[String]): Unit = {
    val foo = new Foo {
      def apply(): Unit = println(s"apply: this: $this")
    }
    print(s"foo: $foo")
    foo.bar()
  }

  type F0 = {
    def apply() : Unit
  }

  class Foo {
    this: F0 =>

    def bar(): Unit = {
      println("call apply")
      apply()
    }
  }
}

While alternative snippets with 'object foo{...}' or explicite subclassing works correctly

package crafts

object Main7 {

  def main(args: Array[String]): Unit = {
    {
      object foo extends Foo {
        def apply(): Unit = println(s"apply: this: $this")
      }
      print(s"foo: $foo")
      foo.bar()
    }
    {
      class Foo_ extends Foo {
        def apply(): Unit = println(s"apply: this: $this")
      }
      val foo = new Foo_
      print(s"foo: $foo")
      foo.bar()
    }
  }

  type F0 = {
    def apply() : Unit
  }

  class Foo {
    this: F0 =>

    def bar(): Unit = {
      println("call apply")
      apply()
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions