Java.lang.String cannot is cast to scala.runtime.Nothing the nothing type in Scala

Source: Internet
Author: User

Often when writing an rdd, such as Val owner_id=row.getas ("owner_id"), etc.,

Run is likely to report an exception: Java.lang.String cannot be cast to scala.runtime.Nothing

After the inspection, it is found that the type should be consistent, should be written: Val owner_id=row.getas[string] ("owner_id")

So, the question arises, why is there an anomaly above? Query the results, think this article a bit of meaning, and then excerpt,

Have done as a study, if infringe your copyright, please comment @dy9776 in the next, I will deal with delete as soon as possible.

Excerpt as follows:

    1. What is nothing?

      Nothing is a type. Just like int, string is a type, such as the value 3 is an int, the value "ABC" is a string type, and int, string is different, there is no value of nothing type.

      Nothing of this type (type) also features a bottom type, the so-called bottom type is the subtype of all other types (sub type) (The bottom type is a subtype of all types.). So you can assume that nothing is a subtype of int and nothing is a subtype of string. The following excerpt from the 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. What is the meaning of nothing
      1. One of the meanings: used for type parameters (covariant parameterized types).
        First, Scala's generics are not allowed without type parameters. The following is excerpted from programming in Scala, 2nd Edition, Chapter Type Parameterization

        Unlike Java, which allows raw types, Scala requires that you specify type parameters.
        (Do not allow raw type,scala like Java to specify type parameters)

        So the following two rows are faulted: Error:type List takes type parameters

        Listval li: List = List(1)

        Rewrite it as follows to compile:

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

        Because the list (1) has an element of type Int: 1, so his type is list[int]. Well, make sense, the type of a list depends on the elements in the list. So list ("abc") is the list[string type, list ("abc", 1) is List[any], because "ABC" and 1 are all subtypes of any type. This is how type deduction works.

        So what about list ()? An empty list, what type is he? Certainly not List[int], because there is no Int, nor List[any], because there is nothing, there is no reason to push the export of any other type, so this list () can only be list[nothing].

        List()l: List[Nothing] = List()

        Nil is inherited from list[nothing]. So can do 1:: Nil and "abc":: Nil. Also because Nil is list[nothing],nothing is Bottome type,list is covariant, so 1:: Nill will be deduced into List[int], "abc":: Nil will be deduced into 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")}

        Small experiment, can you compile and execute it? What is the type of ML1?

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

        meaning of the second, representing the abnormal exit. like throwing exceptions. This is also very made sense, abnormal, certainly nothing will return, type only is nothing.

        throw new Exception("s")e: Nothing

        The nil head method above is also an example. This also shows the benefits of the bottom type of nothing.

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

        Type derivation observes the following procedure and finds that there are 2 possible return types for this ER method, one is int and the other is nothing. Since nothing is a bottom type, the null is a subclass of int, so it is considered that the 2 types that may be returned are int, so the return value of the ER method is deduced as int.

      Said so much, doc on a few words:

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

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

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

      In summary, nothing is a bottom type, and there is no value of any type. There are 2 main uses for type parameters and for non-normal exit.

      Resources:
      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

After reading, a little understand!

Java.lang.String cannot is cast to scala.runtime.Nothing the nothing type in Scala

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.