java.lang.String cannot be cast to scala.runtime.Nothing Scala中的Nothing類型

來源:互聯網
上載者:User

標籤:bottom   font   err   類型推導   imm   define   好處   javascrip   new   

       經常在寫Rdd的時候, 如:  val OWNER_ID=row.getAs("OWNER_ID")  等,

運行是可能會報異常 : java.lang.String cannot be cast to scala.runtime.Nothing

後檢查後發現,其實應該保證類型一致,應該寫成: val OWNER_ID=row.getAs[String]("OWNER_ID") 

 

     那麼,問題來了, 為啥會有上面的異常出現。查詢結果,覺得這篇文章有點意思,就摘抄過來了,

已做為學習,若侵犯您的著作權,請在下評論@dy9776 , 我會儘快處理刪除掉。

 

摘抄如下:

    

    1. Nothing是什麼

      Nothing是一個類型(type)。就像Int、String一樣是一個類型,比如值3是Int型的,值"abc"是String型的,與Int、String不同的是,不存在Nothing類型的值。

      Nothing這個類型(Type)還有個特點,他是Bottom Type,所謂Bottom type就是他是所有其他類型的子類型(Sub Type) (The bottom type is a subtype of all types.)。 所以可以認為Nothing是Int的子類型,Nothing也是String的子類型。下面摘自Scala Reference 3.5.2 Conformance

      For every value type T , scala.Nothing <: T <: scala.Any
      For every type constructor T (with any number of type parameters), scala.Nothing <: T <: scala.Any

    2. Nothing意義何在
      1. 意義之一:用於型別參數(covariant parameterized types)。
        首先,Scala的泛型是不允許不加型別參數的。 以下摘自Programming In Scala, 2nd Edition, Chapter 19 Type Parameterization

        Unlike Java, which allows raw types, Scala requires that you specify type parameters.
        (不像Java那樣允許raw type,Scala必須指定型別參數)

        所以以下兩行會出錯:Error: type List takes type parameters

        type myList = Listval li: List = List(1)

        改寫成如下可以通過編譯:

        type myList = List[String]val li: List[Int] = List(1)

        因為List(1)有一個Int型的元素:1,所以他的類型是List[Int]。嗯,make sense,一個List的類型取決於這個List裡面的元素。所以List(“abc”)那麼就是List[String]型,List(“abc”, 1)就是List[Any],因為"abc"和1都是Any型的子類型。類型推導就是這麼工作的。

        那麼List()呢?一個空的List,他是什麼類型呢?肯定不是List[Int],因為裡面並沒有Int,也不是List[Any],因為裡面啥都沒有,沒有理由推匯出任何一個其他類型,於是這個List()只能是List[Nothing]。

        scala> val l = List()l: List[Nothing] = List()

        Nil就是繼承自List[Nothing]的。所以才可以做1 :: Nil和"abc” :: Nil。也正因為Nil是List[Nothing],Nothing是bottome type,List是協變的,所以1 :: Nill才會被推導成List[Int],“abc” :: Nil會被推導成List[String]

        case object Nil extends List[Nothing] {  override def isEmpty = true  def head: Nothing =    throw new NoSuchElementException("head of empty list") def tail: List[Nothing] = throw new NoSuchElementException("tail of empty list")}

        小實驗,下面可以編譯執行嗎?ml1的類型是什麼呢?

        val ml: List[String] = List()val ml1 = 2 :: ml

        意義之二,代表非正常退出。比如拋異常。這個也很make sense,異常嘛,肯定啥都返回不了,類型只有是Nothing了。

        scala> def e = throw new Exception("s")e: Nothing

        上文中的Nil的head方法也是一個例子。這裡也體現了Nothing的Bottom Type的好處。

        scala> def er(p: Boolean) = if(p) 1 else throw new Exception("yeah")er: (p: Boolean)Int

        類型推導觀察了下程式,發現這個er方法有2種可能的傳回型別,一種是Int,一種是Nothing。由於Nothing是Bottom Type,所以Nothing是Int的子類,所以認為這2種可能返回的類型都是Int,所以推匯出er方法的傳回值是Int。

      說了這麼多,Doc上就幾句話:

      Nothing is - together with scala.Null - at the bottom of Scala‘s type hierarchy.

      Nothing is a subtype of every other type (including scala.Null); there exist no instances of this type. Although type Nothing is uninhabited, it is nevertheless useful in several ways. For instance, the Scala library defines a value scala.collection.immutable.Nil of type List[Nothing]. Because lists are covariant in Scala, this makes scala.collection.immutable.Nil an instance of List[T], for any element of type T.

      Another usage for Nothing is the return type for methods which never return normally. One example is method error in scala.sys, which always throws an exception.

      總結,Nothing是一個Bottom類型,不存在任何值屬於Nothing類型。主要有2個用處,用於型別參數和代表非正常退出。

      參考資料:
      1: http://en.wikipedia.org/wiki/Bottom_type
      2: http://www.scala-lang.org/docu/files/ScalaReference.pdf
      3: http://www.scala-lang.org/api/2.10.4/index.html#scala.Nothing
      4: Programming In Scala, 2nd Edition

 

看完後,有點小明白了! 

java.lang.String cannot be cast to scala.runtime.Nothing Scala中的Nothing類型

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.