"Scala" in Scala, Nothing,null,none,nil

Source: Internet
Author: User

Nothing

Nothing is a subclass of all types, and is also a subclass of NULL. Nothing has objects, but can be used to define types. For example, if a method throws an exception, the return value type of the exception is nothing (although it is not returned).

def get(index:Int):Int = {    if0thrownew Exception(...)    else ....}

If the statement is an expression with a return value, there must be a return value type, if x < 0, throws an exception, the return value of type nothing,nothing is also a subclass of int, so the return type of the IF expression is the return value type of the Int,get method is also int.

Null

Null is a subclass of all Anyref, in Scala's type system, Anyref is a subclass of any, and the any subclass has anyval. All types that correspond to Java value types are subclasses of Anyval. So null can be assigned to all reference types (ANYREF) and cannot be assigned to value types, and the semantics of this Java are the same. Null is the only object that is null.

None

None is an object, which is the subtype of option and is defined as follows:

caseobject None extends Option[Nothing] {  deftrue  defthrownew NoSuchElementException("None.get")}

Scala recommends using Option[x] as the return type in a method that may return empty. Returns SOME[X] If there is a value (some is also the subclass of option), otherwise none is returned, for example:

def get(key: A): Option[B] = {    if (contains(key))        Some(getValue(key))    else        None}

After you have the option, you can use get to get the included value, or use Getorelse to get the default value if IsEmpty is true.

Nil

Nil is an empty list, defined as list[nothing], according to the list definition List[+a], all nil is a subclass of all List[t].

reprint Please indicate the author Jason Ding and its provenance
Gitcafe Blog Home page (http://jasonding1354.gitcafe.io/)
GitHub Blog Home page (http://jasonding1354.github.io/)
CSDN Blog (http://blog.csdn.net/jasonding1354)
Jane Book homepage (http://www.jianshu.com/users/2bd9b48f6ea8/latest_articles)
Google search jasonding1354 go to my blog homepage

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Scala" in Scala, Nothing,null,none,nil

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.