3

I'm trying to implement an abstract, generic Java class in Scala, and I'm getting an error of the form

object creation impossible, since method B in class A of type (x$1: A<Concrete1, Concrete2>#C)Unit is not defined

The library classes that I'm implementing look like this:

public abstract class A<T, U> {
    public void B(C);

    public abstract class C {
        // elided
    }
}

and I want to implement an A to hand back to the library (which will call B, supplying a C). In Java, I can do:

new A<Concrete1, Concrete2>() {
    @Override
    public void B(C c) {
        // implementation
    }
}

In Scala, I'm trying

new A[Concrete1, Concrete2] {
    def B(c: C): Unit = {
        // implementation
    }
}

and I get the error message at the top. Using override, the compiler complains that method B overrides nothing. It seems that the Scala type system isn't recognizing the C I pass in as an A<Concrete1, Concrete2>#C, but I'm not sure what type it does think it is.

I've tried specifying self: A[Concrete1, Concrete2] =>, as well as a self-type on C: def B(c: self.C, but neither solves the problem. I also tried def B(c: A[Concrete1, Concrete2].C), but this raises a syntax error.

Any suggestions?

1 Answer 1

1

Solved: def B(c: A[Concrete1, Concrete2]#C): Unit. Needed the generic type information for this to be a valid override, but I wasn't aware of the # operator.

Sign up to request clarification or add additional context in comments.

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.