我正在尝试使用一些具有默认值的参数来定义一个case类,但默认值需要一个隐式参数。我尝试过这样的事情:
case class ChannelLatches(started: TestLatch, stopped: TestLatch)(implicit system: ActorSystem) {
def this()(implicit system: ActorSystem) = this(new TestLatch(), new TestLatch())(system)
}
和此:
case class ChannelLatches(started: TestLatch, stopped: TestLatch)(implicit system: ActorSystem) {
def this()(implicit system: ActorSystem) = this(new TestLatch(), new TestLatch())(system)
}
但在这两种情况下,编译器都无法识别我的新构造函数。有什么指针吗?
答案 0 :(得分:4)
问题不在case类或其构造函数中。当您收到像
这样的编译错误时scala> val channelLatches = new ChannelLatches
<console>:11: error: could not find implicit value for parameter system: ActorSystem
val channelLatches = new ChannelLatches
^
这意味着你没有一个类型为ActorSystem的隐式变量作为范围内的单个标识符。
你的代码示例(它们是完全相同的代码,对吗?)和@Eastsun的例子都是完全合法的代码:
scala> class ActorSystem
defined class ActorSystem
scala> class TestLatch
defined class TestLatch
scala> case class ChannelLatches(started: TestLatch = new TestLatch, stopped: TestLatch = new TestLatch)(implicit system: ActorSystem)
defined class ChannelLatches
scala> implicit val actor = new ActorSystem
actor: ActorSystem = ActorSystem@586f403e
scala> val channelLatches = new ChannelLatches
channelLatches: ChannelLatches = ChannelLatches(TestLatch@521a74af,TestLatch@46e2b745)
请注意隐式val actor,它使编译器可以隐式提供缺少的参数。
有关隐式参数的介绍,请参阅A Tour of Scala: Implicit Parameters。
---编辑2012-03-05:添加了另一个示例,其中ChannelLatches是Something的内部类
如果你希望ChannelLatches是某个内部类,你真的不需要将ActorSystem实例传递给内部类实例,因为内部类可以访问外部对象的值。 A Tour of Scala: Inner Classes
scala> class ActorSystem
defined class ActorSystem
scala> class TestLatch
defined class TestLatch
scala> class Something(implicit val as: ActorSystem) {
| case class ChannelLatches(started: TestLatch, stopped: TestLatch) {
| def this() = this(new TestLatch(), new TestLatch())
|
| def actorSystem = as
| }
| }
defined class Something
scala> implicit val as = new ActorSystem
as: ActorSystem = ActorSystem@5481be8a
scala> val s = new Something
s: Something = Something@7139acf
scala> val cl = new s.ChannelLatches
cl: s.ChannelLatches = ChannelLatches(TestLatch@38764254,TestLatch@5bfcb5c1)
scala> cl.actorSystem == as
res0: Boolean = true