繁体   English   中英

Scala:错误:缺少参数类型

[英]Scala: error: missing parameter type

我正在尝试编写一些库函数来增强基本集合。 大部分都进​​展顺利,但我遇到了这个问题。

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 <: GenTraversable[A]](self: GenTraversableLike[A, Repr]) = new EnhancedGenTraversableLike[A, Repr](self)

这是我去使用它时会发生什么:

scala> List((1,2),(2,3),(3,4),(2,5)).mapValuesStrict((_:Int).toString)
res0: List[(Int, java.lang.String)] = List((1,2), (2,3), (3,4), (2,5))

scala> List((1,2),(2,3),(3,4),(2,5)).mapValuesStrict(x => x.toString)
<console>:13: error: missing parameter type
              List((1,2),(2,3),(3,4),(2,5)).mapValuesStrict(x => x.toString)
                                                            ^

因此Scala无法确定x的类型。

此答案表明Scala不使用一个参数来解析另一个参数,但是单独的参数列表可以解决问题。 然而,在我的情况下,这并不容易,因为类型信息可以在隐式参数中找到。

有没有办法解决这个问题,这样我每次调用方法时都不必指定类型?


更新:根据Owen的建议,我最终创建了一个特定于可遍历对的丰富类:

class EnrichedPairGenTraversableLike[T, U, Repr <: GenTraversable[(T, U)]](self: GenTraversableLike[(T, U), Repr]) {
  def mapValuesStrict[R, That](f: U => R)(implicit 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 enrichPairGenTraversableLike[T, U, Repr <: GenTraversable[(T, U)]](self: GenTraversableLike[(T, U), 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)

给出“缺少参数类型”。

所以我们想要做的是将隐式参数移动到显式提供的函数“后面”,以便Scala 首先看到隐式参数。 好吧,我们可以这样做的一种方法是添加一个额外的间接层:

class Bar {
    def methWithImplicits2[A](implicit foo: Foo[A]) = new {
        def apply[B](f: A => B) = null
    }
}

(new Bar).methWithImplicits2.apply(x => x)

这是有效的,虽然语法不是那么漂亮。 您可能会考虑使用语法的一种方法是查看当前的设计,看看是否可以隐式进入任何“早期”阶段。 例如,由于mapValuesStrict方法仅在提供隐式时才有意义,因此您可以使隐式的对象属性而不是传递给方法。

但如果在您的设计中不方便,您可以使用额外的隐式转换来隐藏它。 这就是我们想要做的事情:

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]

这只在使用隐式转换时才会发生,这就是为什么我认为这是一个错误。 因此,我们可以进一步收回:(并且,需要-Xexperimental用于依赖方法类型):

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](f: U => R)(implicit 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
    }
}

// The Hack
trait WrappedPairBound[A] {
    type TT
    type UU
    val bound: A <:< (TT, UU)
}

implicit def wrapPairBound[A,T,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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM