问题
I need a builder library that can be called from Scala and Java. Easy enough in Scala using default, named parameters. But how do I call this code from Java? See below. Or perhaps I should go with a fluent API that is more common to both languages?
Scala:
case class Person(gender:Gender.Value, firstName:String, lastName:String){
def fullName = lastName+", "+firstName
override def toString = firstName+","+lastName+","+gender
}
case class PersonBob(
gender:Gender = GenderBob().build,
firstName:String = null,
lastName:String = null) {
def build = Person(
gender,
if(firstName == null) NameBob(if(gender==Gender.Male) engMaleNames
else engFemaleNames).build else firstName,
if(lastName==null) NameBob(engLastNames).build
else lastName
)
}
Usage:
val p1 = PersonBob().build
val p2 = PersonBob(lastName = "Jones").build
Java Usage:
Person p1 = ?
Person p2 = ?
回答1:
default arguments are not interoperable between scala and java, as mentioned in the very last statement from http://www.scala-lang.org/node/2075.
to determine how to use your above code from java, perhaps javap can help. let's take a smaller example than what you have posted. for instance
case class PersonBob(a: String = "aa", b: String = null)
compiling this using scalac, and then running javap on the produced class file (without the .class extension), we get
public class PersonBob extends java.lang.Object implements scala.ScalaObject,scala.Product,scala.Serializable {
public static final java.lang.String apply$default$2();
public static final java.lang.String apply$default$1();
public static final java.lang.String init$default$2();
public static final java.lang.String init$default$1();
public static final scala.Function1 tupled();
public static final scala.Function1 curry();
public static final scala.Function1 curried();
public scala.collection.Iterator productIterator();
public scala.collection.Iterator productElements();
public java.lang.String a();
public java.lang.String b();
public PersonBob copy(java.lang.String, java.lang.String);
public java.lang.String copy$default$2();
public java.lang.String copy$default$1();
public int hashCode();
public java.lang.String toString();
public boolean equals(java.lang.Object);
public java.lang.String productPrefix();
public int productArity();
public java.lang.Object productElement(int);
public boolean canEqual(java.lang.Object);
public PersonBob(java.lang.String, java.lang.String);
}
we can see that the only constructor we have is
public PersonBob(java.lang.String, java.lang.String);
so in your case, your java code would look something like
Person p1 = new PersonBob(new GenderBob().build(), null, null)
Person p2 = new PersonBob(new GenderBob().build(), null, "Jones")
assuming the GenderBob class takes no parameters in its constructor. as for how fluent this in in Java, i suppose ultimately it is a matter of taste, but in my humble opinion i think the Java version could be less verbose for a builder
来源:https://stackoverflow.com/questions/6337444/builder-library-for-scala-and-java