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?

2
  • 2
    Have you tried new A.I? Commented Apr 21, 2015 at 15:47
  • Have you tried A.I? Commented Apr 21, 2015 at 15:48

2 Answers 2

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 { ... }
Sign up to request clarification or add additional context in comments.

Comments

1

This code worked for me:

class MyA extends A {
  def foo() = new A.I {
    def bar = 3
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.