Dei*_*eil 1 scala overloading implicit erasure
lazy val productService = BeanLookup [ProductDataService]
object BeanLookup {
def apply[T](implicit manifest: Manifest[T], context: ActorContext) = {
val beanLookup =
context.actorFor("/user/spring/beanLookup")
Await.result(
(beanLookup.ask(LookupBean(manifest.erasure))(timeout)).mapTo[T](manifest),
timeout.duration) }
def apply[T](implicit manifest: Manifest[T], system: ActorSystem) = {
val beanLookup =
system.actorFor("/user/spring/beanLookup")
Await.result(
(beanLookup.ask(LookupBean(manifest.erasure))(timeout)).mapTo[T](manifest),
timeout.duration) } }
Run Code Online (Sandbox Code Playgroud)
scalac抱怨:
scala: ambiguous reference to overloaded definition,
both method apply in object BeanLookup of type (implicit manifest: Manifest[com.tooe.core.service.LocationCategoryDataService], implicit system: akka.actor.ActorSystem)com.tooe.core.service.LocationCategoryDataService
and method apply in object BeanLookup of type (implicit manifest: Manifest[com.tooe.core.service.LocationCategoryDataService], implicit context: akka.actor.ActorContext)com.tooe.core.service.LocationCategoryDataService
Run Code Online (Sandbox Code Playgroud)
0__*_*0__ 10
简而言之
trait Foo; trait Bar
object Test {
def apply(implicit foo: Foo) {}
def apply(implicit bar: Bar) {}
}
Test.apply // ambiguous
Run Code Online (Sandbox Code Playgroud)
Scala不会通过确定两个implicits中是否只有一个可用来解决重载问题.
如果你想在这种情况下超载,你应该使用磁铁模式.
object FooOrBar {
implicit def fromFoo(implicit f: Foo) = FooOrBar(Left(f))
implicit def fromBar(implicit b: Bar) = FooOrBar(Right(b))
}
case class FooOrBar(e: Either[Foo, Bar])
object Test {
def apply(implicit i: FooOrBar) = i.e match {
case Left (f) => "foo"
case Right(b) => "bar"
}
}
Test.apply // could not find implicit value for parameter i: FooOrBar
implicit val f = new Foo {}
Test.apply // "foo"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1973 次 |
| 最近记录: |