Scala getters and setters in Java class

2020-04-02 08:58发布

问题:

I would like to create a Java class that follows the Scala setters/getters convention.

I tried following simple class, but it does not work:

public class JavaA {
private int a = 0;

public int a() {
    return a;
}

public void a_$eq(int a) {
    this.a = a;
}
}

But when I try to access it from scala:

val x = new JavaA
x.a = 1

and I get "reassignment to val" error message. I tried to look for this, but all issues I found where the other way around from scala to java.

What is the right way to do it?

Thanks!

回答1:

You can only sort of do this, and it's hard enough that you probably don't want to.

What you can't do is write a bare Java class that magically is interpreted as Scala getters and setters. The reason is that Scala embeds information into the class file that it requires for its getters and setters (e.g. are there zero parameter blocks or one empty parameter block--a distinction which is not preserved on the JVM (or in Java)).

What you can do is use Java to implement a Scala-defined interface (i.e. trait):

// GetSetA.scala
trait GetSetA { def a: Int; def a_=(a: Int): Unit }

// JavaUsesGSA.java
public class JavaUsesGSA implements GetSetA {
  private int a = 0;
  public int a() { return a; }
  public void a_$eq(int a) { this.a = a; }
}

What you can't do, even so, is use the class directly (again because Java does not add the appropriate annotation information for Scala):

scala> j.a = 5
<console>:8: error: reassignment to val
       j.a = 5

but since it does implement the trait successfully, you can use it as desired when it is typed as the trait:

scala> (j: GetSetA).a = 5
(j: GetSetA).a: Int = 5

So it's rather a mixed bag. Not perfect by any means, but it may be sufficiently functional to help out in some cases.

(The other alternative, of course, is to provide an implicit conversion from the Java class to one that has a getter/setter that references the real methods on the Java class; this works even when you can't have the Java inherit from Scala.)

(Edit: Of course there's no critical reason that the compiler must act this way; one could argue that interpreting Java-defined getter/setter pairs as if they were Scala ones (i.e. if the classfile does not explicitly say it's from Scala) is a good candidate for a feature enhancement to improve Java interoperability.)



回答2:

I’m afraid you can’t. In Scala, the accessor has to be method with no parameter list, like def a = _a. Writing e.g. def a() = _a in Scala would cause the same error, and there is no way that you can define a method with no parameter list in Java. You may be able to fool the Scala compiler by generating your own ScalaSignature, but that's probably not worth the trouble…