以下は、http: //www.tikalk.com/java/blog/type-safe-builder-scala-using-type-constraintsで説明されている、Scalaのタイプセーフで流動的なビルダーパターンです。これは、ScalaおよびJava用のビルダーライブラリに似ていますが、特にコンパイル時のビルダーチェックを扱います。これはJavaからどのように呼び出すことができますか?「scala.Predef$$eq $ Colon $ eq」パラメーターを指定して、ScalaおよびJava用のクリーンなAPIを使用して実行できますか?
sealed trait TBoolean
sealed trait TTrue extends TBoolean
sealed trait TFalse extends TBoolean
class Builder[HasProperty <: TBoolean] private(i: Int) {
protected def this() = this(-1)
def withProperty(i: Int)(implicit ev: HasProperty =:= TFalse) = new Builder[TTrue](i)
def build(implicit ev: HasProperty =:= TTrue) = println(i)
}
//javap output
public class Builder extends java.lang.Object implements scala.ScalaObject{
public Builder withProperty(int, scala.Predef$$eq$colon$eq); //How is this called from Java?
public void build(scala.Predef$$eq$colon$eq);
public Builder();
}
object Builder {
def apply() = new Builder[TFalse]
}