2

Say I have the following legacy java defined:

abstract class A {

  abstract I foo();

  public interface I
  {
    int bar();
  }
}

And I want to implement this in scala something like the following:

class MyA extends A {

  def foo() = new I {

    def bar = 3

  }
}

The scala will not compile with the error

not found: type I

How can I refer to the java interface I in my scala code?

user79074
  • 4,937
  • 5
  • 29
  • 57

2 Answers2

2

Looking at your java code through scala-colored lenses, you'll see

  • a class A with an abstract method foo,
  • an object A with a single interface in it, A.I.

Since the companion's members are not auto-imported inside the class, you'll need to use A.I or import it first:

def foo() = new A.I { ... }

// or with an import
import A.I
def foo() = new I { ... }
Community
  • 1
  • 1
gourlaysama
  • 11,240
  • 3
  • 44
  • 51
1

This code worked for me:

class MyA extends A {
  def foo() = new A.I {
    def bar = 3
  }
}
pedrofurla
  • 12,763
  • 1
  • 38
  • 49