Skip to content

Scala's java interop disallows access to Scala nested class from Java #10490

@jvican

Description

@jvican

Imagine you're compiling in mixed mode and you have a cyclic dependency between Java and Scala code. In particular, Java wants to access a nested class defined in an Scala class. This is often necessary when you want to define some field or method as static. Let's assume they're defined in the same package java.interop.bug.

package java.interop.bug;
public class StaticsHolder {
  // Foo is defined in Scala
  public static final Foo.Bar bar = new Bar();
}
package java.interop.bug

class Foo {
  class Bar
}

class PerformantPrinter {
  // just a small example to illustrate the Scala -> Java dep
  println(StaticsHolder.bar)
}

If you compile this in mixed mode, first scalac will run. Scalac will parse the java signature and will give the following compile error:

not found: value Foo
      public static final Foo.Bar bar = new Bar();
                                  ^

This happens because Scalac assumes that Bar is defined in a companion object (value) named Foo. This assumption is incorrect because it doesn't take into account that Bar could also be a nested class defined in Foo.

I think this should be easy to fix, but I may be wrong. I've run into this issue twice: now, hacking on statistics in Scalac; and in the past while hacking on Zinc's Java API, which is also compiled in mixed mode.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions