I’m trying to implement this:
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)
}
The compiler gives me error:
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)
I havn’t had the scala type system all figured out. what is wrong with this?
Thanks
EDIT
Fixed by changing ConjunctionBuilder to have only one parameter type L:
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)
But could someone explain why with the R parameter type, the compile fails?
It fails when
ConjunctionBuilderhas theRtype parameter simply because when applying the implicit conversionrestrictionToConjunctionBuilder, the compiler can only inferL(from the parameterrestriction).The type parameter
Rdoes not appear anywhere in the parameter list, so there is no way it can be inferred.When type parameters cannot be inferred, you have to pass them explicitly (but of course in the case of implicit conversion, this defeats the purpose).
By example: the following compiles correctly:
Short of explictly specifying the type parameters, the compiler cannot bind
R, and thus cannot prove that the parameter to the methodandis of the right type.Indeed, the method expects a
Restriction[R], and we give it aRestriction[Int]. This can only match ifR==Int, which the compiler can’t prove asRis unbound.Your fix is totally correct: move the type parameter
Rto the definition ofand. This way when applying the implicit conversionrestrictionToConjunctionBuilder, the compiler can fully infer all the type parameters (that is, the uniqye type parameterL) from the parameterrestriction.Then, when applying
andit can inferRfrom its parameterrhs