Scala getters and setters in Java class

若如初见. 提交于 2019-12-01 15:27:35

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.)

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…

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!