What are the differences between asInstanceOf[T] and (o: T) in Scala?

后端 未结 4 1053
南笙
南笙 2020-11-30 00:40

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

foo.asInstanceOf[Bar]
(foo: Bar)

When I tried, I found that asInstanceOf

4条回答
  •  既然无缘
    2020-11-30 00:49

    Pelotom's answer covers the theory pretty nice, here are some examples to make it clearer:

    def foo(x: Any) {
      println("any")
    }
    
    def foo(x: String) {
      println("string")
    }
    
    
    def main(args: Array[String]) {
      val a: Any = new Object
      val s = "string"
    
      foo(a)                       // any
      foo(s)                       // string
      foo(s: Any)                  // any
      foo(a.asInstanceOf[String])  // compiles, but ClassCastException during runtime
      foo(a: String)               // does not compile, type mismatch
    }
    

    As you can see the type ascription can be used to resolve disambiguations. Sometimes they can be unresolvable by the compiler (see later), which will report an error and you must resolve it. In other cases (like in the example) it just uses the "wrong" method, not that you want. foo(a: String) does not compile, showing that the type ascription is not a cast. Compare it with the previous line, where the compiler is happy, but you get an exception, so the error is detected then with the type ascription.

    You will get an unresolvable ambiguity if you also add a method

    def foo(xs: Any*) {
      println("vararg")
    }
    

    In this case the first and third invocation of foo will not compile, as the compiler can not decide if you want to call the foo with a single Any param, or with the varargs, as both of them seems to be equally good => you must use a type ascription to help the compiler.

    Edit see also What is the purpose of type ascription in Scala?

提交回复
热议问题