【问题标题】:Why does scala compiler fail to find implicit parameter value/conversion when it is an overload and has generic type param?为什么scala编译器在重载并且具有泛型类型参数时找不到隐式参数值/转换?
【发布时间】:2011-11-04 20:36:56
【问题描述】:

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

【问题讨论】:

    标签: scala implicit-conversion implicits


    【解决方案1】:

    第二个隐式隐藏第一个。为什么会有人猜测,您可能会为此打开一个问题(在验证之前没有报告过这个问题之后),但这可能只是将扳手扔进类型推断工作的那些事情之一。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多