简体   繁体   中英

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

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

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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