简体   繁体   English

为什么scala编译器在重载并且具有泛型类型参数时无法找到隐式参数值/转换?

[英]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.

相关问题 当显式转换有效时,为什么此Scala隐式转换失败? - Why does this Scala implicit conversion fail when explicit conversion works? 为什么Scala隐式解析对于带有类型参数的重载方法失败? - Why does Scala implicit resolution fail for overloaded method with type parameter? Scala类型参数化,Shapeless-找不到参数Generic的隐式值 - Scala type parameterization, Shapeless - could not find implicit value for parameter Generic 为什么Scala编译器因缺少JavaSparkContext过滤器的参数类型而失败? - Why does the Scala compiler fail with missing parameter type for filter with JavaSparkContext? 为什么 Scala 找不到可用的隐式视图? - Why does Scala fail to find implicit view when one is available? 在Scala中为隐式参数隐式转换通用容器 - Implicit conversion of a generic container for an implicit parameter in Scala 为什么Scala的编译器不能使用继承类型的隐式转换? - Why Scala's compiler is not able to use an implicit conversion of an inherited type? Scala编译器如何执行隐式转换? - How does the Scala compiler perform implicit conversion? 了解Scala隐式转换和泛型类型推断 - Understanding Scala implicit conversion and generic type inference 为什么Scala在这个特定情况下找不到二级隐含值? - Why does Scala fail to find a secondary implicit value in this one particular case?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM