Despite there is lot of similar questions around this topic I cannot find any appropriate. This is about commonly used builder + entity approach such as Apache commons CLI OptionBuilder + Option and corresponding client code migration to Scala.
Illustration in code:
Java classes:
public class Entity {
private int f1;
private int f2;
public Entity(int f1, int f2) {
this.f1 = f1;
this.f2 = f2;
}
}
public class Builder {
private static int f1 = 0;
private static int f2 = 0;
private static Builder instance = new Builder();
static Builder hasF1(int f1) {
Builder.f1 = f1;
return Builder.instance;
}
static Builder hasF2(int f2) {
Builder.f2 = f2;
return Builder.instance;
}
static Entity create() {
return new Entity(Builder.f1, Builder.f2);
}
}
Normal Java code using this approach:
Entity entity = Builder.hasF1(5).hasF2(10).create();
If this is 'fed' into scalac it complains about Builder not having hasF2 field. The point as from my investigation is Scala has no problem to locate Builder.hasF1 method as class level one but after this it needs to call static hasF2 for existing Java class istance and it has problems.
Any idea how this should be properly resolved?
UPDATE: Related question illustrating all mechanics: Scala error compiling OptionBuilder