私はこれを実装しようとしています:
def buildQuery() {
val restrictions: ConjunctionRestriction[String, Int] =
("name" is "Some One") and ("age" is 20)
}
implicit def stringToEqualsRestrictionBuilder[T](fieldName: String)
: EqualsRestrictionBuilder[T] =
new EqualsRestrictionBuilder[T](fieldName)
implicit def restrictionToConjunctionBuilder[L,R](restriction: Restriction[L])
: ConjunctionBuilder[L,R] =
new ConjunctionBuilder[L,R](restriction)
case class Restrictions(restrictions: Restriction[_]*)
trait Restriction[T] {
def toString: String
}
class EqualsRestriction[T](val fieldName: String, val value: T)
extends Restriction[T] {
override def toString = fieldName + "=" + value
}
class ConjunctionRestriction[A,B](val lhs: Restriction[A],
val rhs: Restriction[B])
extends Restriction[(A,B)] {
override def toString = "(" + lhs + ") AND (" + rhs + ")"
}
class EqualsRestrictionBuilder[T](val fieldName: String,
val restriction: Option[Restriction[T]] = None) {
def is(value: Int) =
new EqualsRestriction[Int](fieldName, value)
def is(value: String) =
new EqualsRestriction[String](fieldName, "\"" + value + "\"")
}
class ConjunctionBuilder[L,R](val lhs: Restriction[L]) {
def and(rhs: Restriction[R]) = new ConjunctionRestriction[L,R](lhs, rhs)
}
コンパイラは私にエラーを与えます:
error: type mismatch;
found : MyOuterClass.this.EqualsRestriction[Int]
required: MyOuterClass.this.Restriction[R]
val restrictions: ConjunctionRestriction[String, Int] =
("name" is "Some One") and ("age" is 20)
私は、scala 型システムをすべて把握していません。これの何が問題なのですか?
ありがとう
編集
ConjunctionBuilder を変更して、パラメータ タイプ L を 1 つだけ持つように修正しました。
class ConjunctionBuilder[L](val lhs: Restriction[L]) {
def and[R](rhs: Restriction[R]) = new ConjunctionRestriction[L,R](lhs, rhs)
}
implicit def restrictionToConjunctionBuilder[L](restriction: Restriction[L])
: ConjunctionBuilder[L] =
new ConjunctionBuilder[L](restriction)
しかし、誰かが R パラメータ型でコンパイルが失敗する理由を説明できますか?