[英]Why does scala compiler fail to find implicit parameter value/conversion when it is an overload and has generic type param?
Scala 2.8.1 Scala 2.8.1
Take the following class hierarchy 采用以下类层次结构
abstract class A
class B extends A
class C extends A
Why is the scala compiler unable to find the implicit parameter for send
when sending an instance of B
below 为什么scala编译器在下面发送B
实例时无法找到send
的隐式参数
implicit def routingKeyFor[T <: A](value: T) =
value.getClass.getSimpleName
implicit def routingKeyFor(value: C) = "custom C"
def send[T <: A](value: T)(implicit createRoutingKey: T => String):
Validation[Throwable, String] = Success(createRoutingKey(value))
val resultOfSendingB = send(new B)
val resultOfSendingC = send(new C)
Why is the compiler able to locate the value for the implicit parameter when the generic version of routingKeyFor
is renamed? 为什么在重命名routingKeyFor
的通用版本时,编译器能够找到隐式参数的值?
implicit def someOtherName[T <: A](value: T) =
value.getClass.getSimpleName
The second implicit is shadowing the first one. 第二个隐含的是遮蔽第一个。 Why is anyone's guess, and you might open an issue for it (after verifying that this wasn't reported before), but it might just be one of those things that throw a spanner into the works of type inference. 为什么有人在猜测,你可能会为它打开一个问题(在验证之前没有报告之后),但它可能只是将扳手引入类型推断的工作之一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.