繁体   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

采用以下类层次结构

abstract class A

class B extends A

class C extends A

为什么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)

为什么在重命名routingKeyFor的通用版本时,编译器能够找到隐式参数的值?

implicit def someOtherName[T <: A](value: T) = 
  value.getClass.getSimpleName

第二个隐含的是遮蔽第一个。 为什么有人在猜测,你可能会为它打开一个问题(在验证之前没有报告之后),但它可能只是将扳手引入类型推断的工作之一。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM