Reputation: 837
I am having trouble compiling a function due to implicit conversions. I have the following base case classes:
sealed abstract class Expr0[T](implicit ev: VectorSpace0[T]) extends ID {...
case class Neg0[T](e: Expr0[T])(implicit ev: VectorSpace0[T]) extends Expr0[T] { ...
Then in an object I have the following function
def simplify[T](e: Expr0[T])(implicit ev: VectorSpace0[T]): Expr0[T] = {
def s(expr: Expr0[T])(implicit ev: VectorSpace0[T]): Result[Boolean, T] = expr match {
case Neg0(e) =>
val re = s(e)
val ne = Neg0.apply(re.e)
if (re.r) new TR(ne) else FR(ne)
The code above compiles correctly with no problem. Now I want to create a function that will execute that sequence of statements in the case. So I created the following helper function:
def C[T](f: Expr0[T] => Expr0[T], re: Result[Boolean, T])(implicit ev: VectorSpace0[T]) = {
val ne = f(re.e); if (re.r) new TR(ne) else FR(ne)
}
And now I try to use so:
def simplify[T](e: Expr0[T])(implicit ev: VectorSpace0[T]): Expr0[T] = {
def s(expr: Expr0[T])(implicit ev: VectorSpace0[T]): Result[Boolean, T] = expr match {
case Neg0(e) =>
C(Neg0.apply, s(e))
and I get the error:
/src/main/scala/ann/unit/Expr0.scala:412: No member of type class ann.uinit.VectorSpace in scope for T
C(Neg0.apply, s(e))
^
I have been poking and prodding this for several hours but no luck. I think the problem here is with the definition of C[T]
(3rd code snippet). Maybe I have to add something to the definition of the first parameter f
, which is a function, so that the implicit T
is correctly determined.
Can anyone advise me on how to correct or further diagnose this problem?
TIA
Upvotes: 1
Views: 93
Reputation: 67300
It looks like a bit of a mess there with the nested implicits of the same type and name. The message might be misleading, perhaps the eta expansion in Neg0.apply is the culprit, perhaps a missing type inference for T in C[T].
I suggest you make that call explicit first and see if the error narrows down:
C[T](Neg0[T](_), s(e))
Upvotes: 1