Scala 中的 asInstanceOf[T] 和 (o: T) 有什么区别?

Scala 中的 asInstanceOf[T] 和 (o: T) 有什么区别?

问题描述:

我看到在 Scala 中有两种方法可以转换对象:

I saw that there are two methods to cast an object in Scala:

foo.asInstanceOf[Bar]
(foo: Bar)

当我尝试时,我发现 asInstanceOf 不使用隐式转换,而另一个使用隐式转换.

When I tried, I found that asInstanceOf doesn't use implicit conversion whereas the other one does.

这两种方法在行为上有什么区别?建议在哪些地方使用一个?

What are the differences of behavior between these two methods? And where is it recommended to use one over the other?

  • foo.asInstanceOf[Bar] 是一种 cast 类型,主要是一个运行时操作.它说编译器应该被强制相信 foo 是一个 Bar.如果在运行时 foo 被评估为不是 Bar 的东西,这可能会导致错误(ClassCastException).

    • foo.asInstanceOf[Bar] is a type cast, which is primarily a runtime operation. It says that the compiler should be coerced into believing that foo is a Bar. This may result in an error (a ClassCastException) if and when foo is evaluated to be something other than a Bar at runtime.

      foo:Bar 是一种 ascription 类型,它完全是一个编译时操作.这为编译器提供了理解代码含义的帮助,而不会强迫它相信任何可能不真实的东西;使用类型归属不会导致运行时故障.

      foo:Bar is a type ascription, which is entirely a compile-time operation. This is giving the compiler assistance in understanding the meaning of your code, without forcing it to believe anything that could possibly be untrue; no runtime failures can result from the use of type ascriptions.

      类型归属也可用于触发隐式转换.例如,您可以定义以下隐式转换:

      Type ascriptions can also be used to trigger implicit conversions. For instance, you could define the following implicit conversion:

      implicit def foo(s:String):Int = s.length
      

      然后像这样确保它的使用:

      and then ensure its use like so:

      scala> "hi":Int                                 
      res29: Int = 2
      

      将类型 Int 赋予 String 通常是编译时类型错误,但在放弃之前编译器会搜索可用的隐式转换来解决问题离开.将在给定上下文中使用的特定隐式转换在编译时已知.

      Ascribing type Int to a String would normally be a compile-time type error, but before giving up the compiler will search for available implicit conversions to make the problem go away. The particular implicit conversion that will be used in a given context is known at compile time.

      毋庸置疑,运行时错误是不可取的,因此您可以以类型安全方式(不使用asInstanceof)指定事物的程度越好!如果您发现自己在使用 asInstanceOf,那么您可能应该改用 match.

      Needless to say, runtime errors are undesirable, so the extent to which you can specify things in a type-safe manner (without using asInstanceof), the better! If you find yourself using asInstanceOf, you should probably be using match instead.