Scala 原语到 AnyRef 的隐式转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12627241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scala implicit conversion of primitive to AnyRef
提问by Ralph
In Scala code that I am writing, I have a Map[String, AnyRef]. When I try to initialize the Map using the following, Scala complains that it is expecting a Map[String, AnyRef]but the value is a Map[String, Any]:
在我正在编写的 Scala 代码中,我有一个Map[String, AnyRef]. 当我尝试使用以下内容初始化 Map 时,Scala 抱怨它期望 aMap[String, AnyRef]但值为 a Map[String, Any]:
val myMap: Map[String, AnyRef] =
Map("foo" -> true, "bar" -> false)
I know that I can use the following instead:
我知道我可以使用以下内容:
val myMap: Map[String, AnyRef] =
Map("foo" -> true.asInstanceOf[AnyRef], "bar" -> false.asInstanceOf[AnyRef])
I declared the following in scope:
我在范围内声明了以下内容:
implicit def booleanToAnyRef(value: Boolean): AnyRef = value.asInstanceOf[AnyRef]
but the compiler still complains.
但编译器仍然抱怨。
Shouldn't the compiler use the implicit method to convert the primitive boolean values into AnyRefvalues? Is there any way, short of (the ugly) x.asInstanceOf[AnyRef]to have these converted?
编译器不应该使用隐式方法将原始布尔值转换为AnyRef值吗?有没有办法,除了(丑陋的)x.asInstanceOf[AnyRef]让这些转换?
回答by som-snytt
For the record, as the other answers suggest, the latest compiler will say:
作为记录,正如其他答案所暗示的那样,最新的编译器会说:
Note: an implicit exists from scala.Boolean => java.lang.Boolean, but methods inherited from Object are rendered ambiguous. This is to avoid a blanket implicit which would convert any scala.Boolean to any AnyRef. You may wish to use a type ascription:
x: java.lang.Boolean.
注意:scala.Boolean => java.lang.Boolean 中存在一个隐式,但从 Object 继承的方法被渲染为不明确的。这是为了避免将任何 scala.Boolean 转换为任何 AnyRef 的全面隐式。您可能希望使用类型归属:
x: java.lang.Boolean。
The latest compiler will always be a friend who gives better advice than the friend you used to hang with and get into trouble together with.
最新的编译器将永远是一个提供更好建议的朋友,而不是你曾经与之相处并一起陷入困境的朋友。
回答by Sergey Passichenko
You should avoid such implicit conversions between general types (and compiler suggests it). If you want to use java.lang.Booleaninstead of scala.Booleanyou can do it this way:
您应该避免在一般类型之间进行这种隐式转换(并且编译器建议这样做)。如果你想使用java.lang.Boolean而不是scala.Boolean你可以这样做:
import java.lang.Boolean._
val myMap: Map[String, AnyRef] = Map("foo" -> TRUE, "bar" -> FALSE)
回答by om-nom-nom
Just say that you're using java's booleans:
只需说您正在使用 java 的布尔值:
scala> def foo(x: AnyRef) = x.toString
foo: (x: AnyRef)java.lang.String
scala> foo(true: java.lang.Boolean)
res0: java.lang.String = true
or define the following implicit:
或定义以下隐式:
scala> implicit def b2B(x: Boolean) = java.lang.Boolean.valueOf(x)
//foo: (x: Boolean)java.lang.Boolean
scala> foo(true)
//res1: java.lang.Boolean = true
for the numeric types (but not for the Boolean) there is .underlying method:
对于数字类型(但不是布尔值),有 .underlying 方法:
scala> 1.underlying
//res2: AnyRef = 1

