2013-12-22 28 views
1

在斯卡拉的type erasure閱讀這個優秀question/answer後,我試過這段代碼。 Scala編譯器沒有輸出type erasure警告。爲什麼沒有類型擦除警告

scala> val x: List[Int] = List(1,2,3) 
x: List[Int] = List(1, 2, 3) 

scala> x match { 
    | case List(x: Int) => println("a") 
    | case _ => println("false") 
    | } 
false 

沒有上面的代碼輸出相同的警告,這段代碼爲什麼:

scala> List(1,2,3) match { 
    | case l: List[String] => println("list of strings") 
    | case _ => println("ok") 
    | } 
<console>:9: warning: fruitless type test: a value of type List[Int] cannot 
also be a List[String] (but still might match its erasure) 
        case l: List[String] => println("list of strings") 
         ^
    list of strings 

回答

3

第一種情況不只是測試類型 - 這是通過模式匹配測試,該清單有且只有一個整數元素。