Scala的null,nil,Null,Nothing,None,Any

nil :作为 List[T]的初始化,表示一个空数组 官方文档解释

null :表示一个空对象,是Null唯一的实例

Null :不用怎么管这个,基本不会用到。

Nothing and Null

Nothing is a subtype of all types, also called the bottom type. There is no value that has type Nothing. A common use is to signal non-termination such as a thrown exception, program exit, or an infinite loop (i.e., it is the type of an expression which does not evaluate to a value, or a method that does not return normally).

Null is a subtype of all reference types (i.e. any subtype of AnyRef). It has a single value identified by the keyword literal nullNull is provided mostly for interoperability with other JVM languages and should almost never be used in Scala code. We’ll cover alternatives to null later in the tour.

None

None是一个object,是Option的子类型,定义如下 

case object None extends Option[Nothing] {
  def isEmpty = true
  def get = throw new NoSuchElementException("None.get")
}

scala推荐在可能返回空的方法使用Option[X]作为返回类型。如果有值就返回Some[x](Some也是Option的子类),否则返回None,例如 

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

获得Option后,可以使用get获得包含的值,或者使用getOrElse获得默认值如果isEmpty为true。 
 

这个None还没能理解

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值