Dzhu
Dzhu

Reputation: 4371

Scala typing with implicits gives typing error

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?

Upvotes: 2

Views: 133

Answers (1)

Régis Jean-Gilles
Régis Jean-Gilles

Reputation: 32719

It fails when ConjunctionBuilder has the R type parameter simply because when applying the implicit conversion restrictionToConjunctionBuilder, the compiler can only infer L (from the parameter restriction). The type parameter R does 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:

val restrictions: ConjunctionRestriction[String, Int] =
  (("name" is "Some One"): ConjunctionBuilder[String, Int]) and ("age" is 20)

Short of explictly specifying the type parameters, the compiler cannot bind R, and thus cannot prove that the parameter to the method and is of the right type. Indeed, the method expects a Restriction[R], and we give it a Restriction[Int]. This can only match if R == Int, which the compiler can't prove as R is unbound.

Your fix is totally correct: move the type parameter R to the definition of and. This way when applying the implicit conversion restrictionToConjunctionBuilder, the compiler can fully infer all the type parameters (that is, the uniqye type parameter L) from the parameter restriction. Then, when applying and it can infer R from its parameter rhs

Upvotes: 3

Related Questions