[英]Scala: How to apply implicit parameters to the different scope?
I can't find out why my implicits aren't in the scope. 我无法找出为什么我的暗示不在范围内。 Here is a test:
这是一个测试:
package test
object Main extends App {
import fp._
def foo(a: Int)(implicit b: B0) = a + b.x
print(foo(1))
}
Components in diff package: diff包中的组件:
package fp
trait Components {
implicit val b0: B0 = B0(2)
}
object Components extends Components
sealed trait B
case class B0(x: Int) extends B
The error I got 我得到的错误
could not find implicit value for parameter b: fp.B0
print(foo(1))
^
Any ideas how to fix it? 任何想法如何解决它?
You need to either do 你需要做
import fp.Components._
or define the implicit value b0
in an implicit scope such as inside the companion object of B0
or the package object. 或者在隐式范围内定义隐式值
b0
,例如在B0
的伴随对象内或包对象内。
Here are some reading materials: 以下是一些阅读材料:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.