scala 找不到参数 e 的隐含值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34466222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
could not find implicit value for parameter e
提问by Jas
case class Cat(name: String)
object CuterImplicits {
implicit class CatCuteChecker(c: Cat) {
def isCute(c: Cat) = true
}
}
trait CuteChecker[A] {
def isCute(a: A): Boolean
}
object CheckingForCuteness {
def isItCute[A](a: A) = implicitly[CuteChecker[A]].isCute(a)
}
object Main extends App {
CheckingForCuteness.isItCute[Cat](Cat("funny"))
}
how to fix:
怎么修:
Error:(17, 37) could not find implicit value for parameter e: CuteChecker[A] def isItCute[A](a: A) = implicitly[CuteChecker[A]].isCute(a) ^
错误:(17, 37) 找不到参数 e 的隐式值:CuteChecker[A] def isItCute[A](a: A) = implicitly[CuteChecker[A]].isCute(a) ^
回答by 0__
If you use implicitlythat simply makes a value implicitly in scope "explicitly" available. So your isItCutemethod should be either of the following two variants:
如果您使用implicitly它,则只会在“显式”可用范围内隐式地设置一个值。所以你的isItCute方法应该是以下两种变体之一:
def isItCute[A: CuteChecker](a: A) = implicitly[CuteChecker[A]].isCute(a)
def isItCute[A](a: A)(implicit cc: CuteChecker[A]) = cc.isCute(a)
Next you want an implicit instance for Cat. The implicit classdoesn't help you here because it requires a non-implicit value of type Cat. You can see that this approach is wrong because the constructor parameter is never used. You can use an implicit object:
接下来,您需要一个隐式实例Cat。在implicit class这里对您没有帮助,因为它需要一个非隐式的 type 值Cat。您可以看到这种方法是错误的,因为从不使用构造函数参数。您可以使用implicit object:
implicit object CatCuteChecker extends CuteChecker[Cat] {
def isCute(c: Cat) = true
}
Finally, you provide implicits in object CuterImplicits. For them to be visible to Main, you need to import the contents:
最后,您在 object 中提供隐式CuterImplicits。为了让它们可见Main,您需要导入内容:
object Main extends App {
import CuterImplicits._
CheckingForCuteness.isItCute[Cat](Cat("funny"))
}
回答by Clashsoft
There a multiple problems in your situation. The implicitlycall expects an instance of the CuteCheckertrait, while CatCuteCheckeris neither an instance nor does not it extend this trait. Furthermore, the cclass parameter is completely unnecessary.
你的情况有很多问题。该implicitly调用需要CuteCheckertrait 的一个实例,而CatCuteChecker它既不是一个实例,也不扩展这个 trait。此外,c类参数完全没有必要。
You can fix your problem by declaring that subtyping relationship and providing an implicitvalue:
您可以通过声明该子类型关系并提供一个implicit值来解决您的问题:
object CuterImplicits
{
class CatCuteChecker with CuteChecker
{
def isCute(c: Cat) = true
}
implicit val catCuteChecker = new CatCuteChecker
}
回答by Daniel Mahler
Implicits have to be visible unqualifiedat the point of invocation. Different ways that implicits can become visible is best described in this answer: https://stackoverflow.com/a/5598107/843348.
隐式必须在调用点不加限定地可见。在这个答案中最好地描述了隐式变得可见的不同方式:https: //stackoverflow.com/a/5598107/843348。
It is also not completely clear what you are trying to achieve and there are a number of possible ways to achieve something likethe example.
也不完全清楚您要实现的目标,并且有多种可能的方法可以实现类似示例的目标。
One possibility is monkey patching Cat using an implicit class:
一种可能性是使用隐式类对 Cat 进行猴子修补:
case class Cat(name: String)
object CuteImplicits {
implicit class CuteCat(c: Cat){
def isCute = true
}
}
object Main extends App {
import CuteImplicits._
Cat("funny").isCute
}
You put implicits in the companion object of an associated type and the it is visible automatically.
您将隐式放入关联类型的伴随对象中,并且它会自动可见。
case class Cat(name: String)
object Cat {
implicit class CuteCat(c: Cat){
def isCute = true
}
}
object Main extends App {
Cat("funny").isCute
}
In minimal example like this it not clear why you would not build the functionality directly into Cat.
在这样的最小示例中,不清楚为什么不将功能直接构建到Cat.

