Scala 使用case类apply:T的作用域中没有类型为class a的成员
由于隐式转换,我在编译函数时遇到问题。 我有以下基本案例类:Scala 使用case类apply:T的作用域中没有类型为class a的成员,scala,types,implicit,implicit-typing,Scala,Types,Implicit,Implicit Typing,由于隐式转换,我在编译函数时遇到问题。 我有以下基本案例类: 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] { ... 然后在一个对象中,我有以下函数 def simplify[T](e: Expr0[T])(implicit
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] { ...
然后在一个对象中,我有以下函数
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)
上面的代码编译正确,没有问题。现在我想创建一个函数,它将在本例中执行该语句序列。因此,我创建了以下帮助函数:
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)
}
现在我试着用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))
我得到了一个错误:
/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))
^
我已经戳了几个小时了,但是没有运气。我认为这里的问题在于C[T]
(第三段代码片段)的定义。也许我必须在第一个参数f
的定义中添加一些东西,这是一个函数,以便正确地确定隐式T
有人能告诉我如何纠正或进一步诊断此问题吗
TIA它看起来有点乱,有相同类型和名称的嵌套隐式。该消息可能会产生误导,可能是Neg0中的eta扩展。apply是罪魁祸首,可能是C[T]中的T的缺少类型推断 我建议您首先明确该调用,然后查看错误是否缩小:
C[T](Neg0[T](_), s(e))
坦白地说,你有一个相当混乱的嵌套隐式有相同的类型和名称。该消息可能有误导性,可能是
Neg0.apply
中的eta扩展是罪魁祸首,可能是C[T]
中的T
缺少类型推断。我建议您首先显式调用,然后查看错误是否缩小范围:C[T](Neg0[T](u),s(e))
Yes,不需要相同类型和名称的嵌套隐式(重构的残余)。你是对的-埃塔扩展解决了它。在我的实验中,我收到了提示eta的错误消息,但没有点击(不需要显式的T输入)。。。。我如何给解打分?C(Neg0(u0),s(e))有效。不需要类型参数。