scala – 隐式转换的结果类型必须比AnyRef更具体
发布时间:2020-12-16 19:06:58 所属栏目:安全 来源:网络整理
导读:让 def h(a: AnyRef*) = a.mkString(",")h: (a: AnyRef*)String 所以 h("1","2")res: String = 1,2 但是,h(1,2) error: the result type of an implicit conversion must be more specific than AnyRef h(1,2) ^error: the result type of an implicit conve
让
def h(a: AnyRef*) = a.mkString(",") h: (a: AnyRef*)String 所以 h("1","2") res: String = 1,2 但是,h(1,2) error: the result type of an implicit conversion must be more specific than AnyRef h(1,2) ^ error: the result type of an implicit conversion must be more specific than AnyRef h(1,2) ^ 这至少在Scala 2.11.1和2.11.1中. 解决方法
您可以使用以下方式重现问题:
val x: AnyRef = 42 这是相关的pull request on github介绍的变化 理由是为了安全起见,一些隐式转换被明确禁用,即当转换从T到U被禁用: T <: Null 要么 AnyRef <: U 在你的具体情况下,这意味着Int(这不是AnyRef)永远不会被转换为AnyRef. 如果您需要同时接受Int和String,您可以考虑接受Any.由于每个Scala对象从Any继承,所以不需要隐式转换. def h(a: Any*) = a.mkString(",") (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |