gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:38:37 24 4
gpt4 key购买 nike

斯卡拉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 - 为什么 scala 编译器在重载且具有泛型类型参数时无法找到隐式参数值/转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8015431/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com