如何在 Scala 中正确使用 asInstanceOf [英] how to use asInstanceOf properly in Scala
问题描述
我一直在使用基本的 Scala 数据类型.我注意到 scala.Any
类定义了来自 这里 API 可以将接收器对象转换为 T0 类型".使用这种方法作为起点,我想研究 Scala 中的转换.另外,我在 stackoverflow 上查找了关于这个主题的其他问题,我想出了 这个有了这些信息,我编写了一个愚蠢的程序.
I have been playing with basic Scala data types. I noticed that the scala.Any
class defines the method asInstanceOf[T0]: T0
from here The API has it that it can "Cast the receiver object to be of type T0". Using this method as a starting point, I wanted to investigate casting in Scala. Also, I looked up stackoverflow for other questions on this topic and I came up with this
With this information in hand, I wrote up a silly program.
package com.att.scala
import com.att.scala.Sheltie
object Casting {
//def foo(x: String){
def foo(x: Int) {
println("x is " + x)
//if(x.isInstanceOf[String])
if(x.isInstanceOf[Int])
println("Int x is " + x)
//println("String x is " + x)
}
def entry() {
//val double: Any = 123.123
val double: Double = 123.23
val int = double.asInstanceOf[Int] //exception expected here
//val str: String = "123"
foo(int)
}
}
<小时>
我的目标是了解在以下情况下会发生什么(以及为什么):1) 从 Any 类型转换为 Int.2) 从 Double 类型转换为 Int3) 从 String 到 Int 的转换
My goal is to understand what happens (and why) in the following cases: 1) casting from an Any type to an Int. 2) casting from Double type to an Int 3) casting from String to Int
在第一种情况下,当我将程序运行为 com.att.scala.Casting.entry 时,我得到了一个运行时 ClasscastException,如下所示.异常如下所示:
In the first case, I got a run time ClasscastException as below, when I ran the program as - com.att.scala.Casting.entry. The exception is shown below:
java.lang.ClassCastException: java.lang.Double 不能转换为 java.lang.Integer在 scala.runtime.BoxesRunTime.unboxToInt(来源不明)在 com.att.scala.Casting$.entry(Casting.scala:17)
在第二种情况下,我得到以下结果:整数是 123x 是 123整数 x 是 123
In the second case, I get the following result: int is 123 x is 123 Int x is 123
在这种情况下,代码应该产生 ClasscastException,但它没有.这就是我的担心.
In this case, the code is supposed to produce a ClasscastException, but it does not. That is my worry.
- 在第三种情况下,我得到一个 classcastexception:
java.lang.ClassCastException: java.lang.String 不能转换为 java.lang.Integer在 scala.runtime.BoxesRunTime.unboxToInt(来源不明)在 com.att.scala.Casting$.entry(Casting.scala:20)
通过此示例,我的目标是了解 Scala 中强制转换的基础知识.我知道这个例子无论如何都不是真实世界的例子,但我试图让我的头脑围绕基础知识.
With this example,my goal is to get to the very basics of casting in Scala. I know that this example is not a real world example by any means, but I was trying to get my head wrapped around the basics.
推荐答案
Java(和 Scala)允许您将原始 double
转换为 int
(在 Scala 的情况下,Double
到 Int
).另一方面,您不能将 java.lang.Double
转换为 java.lang.Int
.
Java (and Scala) allows you to cast a primitive double
to int
(in Scala's case, Double
to Int
). On the other hand, you cannot cast java.lang.Double
to java.lang.Int
.
当您将 Double
声明为 Any
时,您明确要求编译器忘记您给它一个 Double
.因此,为了支持 Any
接口,编译器将值存储为装箱双精度值(即 java.lang.Double
).
When you declare the Double
as an Any
, you're explicitly asking the compiler to forget that you gave it a Double
. Therefore, in order to support the Any
interface, the compiler is storing the value as a boxed double (i.e., java.lang.Double
).
这种行为看起来确实令人困惑,但这不是错误.根据 Scala 语言规范的 §12.1:
The behavior does seem confusing, but it's not a bug. According §12.1 of the Scala Language Spec:
如果 T 是数值,则测试 x.asInstanceOf[T] 被特殊对待类型(第 12.2 节).在这种情况下,演员表将被翻译成一个应用程序转换方法 x.toT(第 12.2.1 节).
The test x.asInstanceOf[T] is treated specially if T is a numeric value type (§12.2). In this case the cast will be translated to an application of a conversion method x.toT (§12.2.1).
这篇关于如何在 Scala 中正确使用 asInstanceOf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!