Scala getters and setters in Java class

前端 未结 2 1855
故里飘歌
故里飘歌 2021-01-17 14:13

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 c         


        
2条回答
  •  爱一瞬间的悲伤
    2021-01-17 14:43

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

提交回复
热议问题