Scala:错误:缺少参数类型
| 
 我正在尝试编写一些库函数来增强基本集合.大部分都进??展顺利,但我遇到了这个问题. 
  
  
  class EnhancedGenTraversableLike[A,Repr <: GenTraversable[A]](self: GenTraversableLike[A,Repr]) {
  def mapValuesStrict[T,U,R,That](f: U => R)(implicit ev: A <:< (T,U),bf: CanBuildFrom[Repr,(T,R),That]) = {
    val b = bf(self.asInstanceOf[Repr])
    b.sizeHint(self.size)
    for ((k: T,v: U) <- self) b += k -> f(v)
    b.result
  }
}
implicit def enhanceGenTraversableLike[A,Repr]) = new EnhancedGenTraversableLike[A,Repr](self)这是我去使用它时会发生什么: scala> List((1,2),(2,3),(3,4),5)).mapValuesStrict((_:Int).toString)
res0: List[(Int,java.lang.String)] = List((1,5))
scala> List((1,5)).mapValuesStrict(x => x.toString)
<console>:13: error: missing parameter type
              List((1,5)).mapValuesStrict(x => x.toString)
                                                            ^因此Scala无法确定x的类型. This answer表示Scala不使用一个参数来解析另一个参数,但单独的参数列表可以解决问题.然而,在我的情况下,这并不容易,因为类型信息可以在隐式参数中找到. 有没有办法解决这个问题,这样我每次调用方法时都不必指定类型? 更新:根据Owen的建议,我最终创建了一个特定于可遍历对的丰富类: class EnrichedPairGenTraversableLike[T,Repr <: GenTraversable[(T,U)]](self: GenTraversableLike[(T,Repr]) {
  def mapValuesStrict[R,That](f: U => R)(implicit bf: CanBuildFrom[Repr,v: U) <- self) b += k -> f(v)
    b.result
  }
}
implicit def enrichPairGenTraversableLike[T,Repr]) = new EnrichedPairGenTraversableLike(self)解决方法
 就在这里.让我举一个简单的例子.我希望这也适用 
  你更复杂的用例. 说我们有 trait Foo[A]
class Bar {
    def methWithImplicits[A,B](f: A => B)(implicit foo: Foo[A]) = null
}
implicit def fooInt: Foo[Int] = null现在这完全是你描述的问题,因为 (new Bar).methWithImplicits(x => x) 给出“缺少参数类型”. 所以我们想要做的是将隐式参数移到“后面” class Bar {
    def methWithImplicits2[A](implicit foo: Foo[A]) = new {
        def apply[B](f: A => B) = null
    }
}
(new Bar).methWithImplicits2.apply(x => x)这是有效的,虽然语法不是那么漂亮.你可以考虑的一种方式 但如果在您的设计中不方便,您可以使用额外的隐含 implicit def addFoo[A](bar: Bar)(implicit foo: Foo[A]) = new {
    def methWithImplicits3[B](f: A => B) = null
}但不幸的是,我怀疑是Scala中的一个错误导致它 could not find implicit value for parameter foo: test.Foo[A] 这只发生在使用隐式转换时,这就是为什么我认为它是一个 trait FooWrapper {
    type AA
    val foo: Foo[AA]
}
implicit def wrapFoo[A](implicit theFoo: Foo[A]) = new FooWrapper {
    type AA = A
    val foo = theFoo
}
implicit def addFoo(bar: Bar)(implicit foo: FooWrapper) = new {
    def methWithImplicits3[B](f: foo.AA => B) = null
}现在 (new Bar).methWithImplicits3(x => x) 效果很好;) 更新 在你的特定情况下,我认为你最好的办法是将隐含的工作加入到EnhanceGenTraversable中,但是,唉,需要同样的黑客来解决可能的错误: // Notice `ev` is now a field of the class
class EnhancedGenTraversableLike[A,Repr <: GenTraversable[A],T,U]
    (self: GenTraversableLike[A,Repr],ev: A <:< (T,U))
{
    def mapValuesStrict[R,That]) = {
        val b = bf(self.asInstanceOf[Repr])
        b.sizeHint(self.size)
        for ((k: T,v: U) <- self) b += k -> f(v)
        b.result
    }
}
// The Hack
trait WrappedPairBound[A] {
    type TT
    type UU
    val bound: A <:< (TT,UU)
}
implicit def wrapPairBound[A,U](implicit ev: A <:< (T,U)) = new WrappedPairBound[A] {
    type TT = T
    type UU = U
    val bound = ev
}
// Take the implicit here
implicit def enhanceGenTraversableLike[A,Repr <: GenTraversable[A]]
        (self: GenTraversableLike[A,Repr])(implicit ev: WrappedPairBound[A]) =
    new EnhancedGenTraversableLike[A,Repr,ev.TT,ev.UU](self,ev.bound)(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! | 
