scalagen icon indicating copy to clipboard operation
scalagen copied to clipboard

Conversion drops a constructor

Open dnadolny opened this issue 11 years ago • 3 comments

Starting from the Java:

class A {
    public A(String str) {}
}
class B extends A {
    public B() {
        super("some value");
    }
    public B(String str) {
        super(str);
    }
}

Converting the class B gives:

  class B extends A {
    def this(str: String) {
      super(str)
    }
  }

The default constructor that called super("some value") is silently dropped.

dnadolny avatar Jun 18 '13 02:06 dnadolny

Another case of a dropped constructor:

public class A extends B<String, Map<String, Double>> {
   private static final long serialVersionUID = 1L;

   public A() {
      super(new C<Map<String, Double>>() {
         public D<String, Double> z() {
            return new E<String, Double>(0.0);
         }
      });
   }
}

becomes:

@SerialVersionUID(1L)
class A extends B[String, Map[String, Double]]

dnadolny avatar Jun 18 '13 02:06 dnadolny

The constructor usage optimization is not yet perfect, the sources are here https://github.com/mysema/scalagen/blob/master/scalagen/src/main/scala/com/mysema/scalagen/Constructors.scala

I don't think there is a valid way to convert the first example to Scala, but the second example should work

timowest avatar Jun 20 '13 19:06 timowest

Technically the first one could be (class B(str: String) extends A(str) { def this() = this("some value") }), although to do that would require knowing that the literal "some value" is a String, and that the B(String str) constructor doesn't do anything not done by B() (which, in this case, is just nothing). It's probably not worth it to attempt to fix it, but how about putting a comment /* Unconverted constructor: (original Java here, or at least the signature) */, or something to that effect?

dnadolny avatar Jun 20 '13 19:06 dnadolny