idea没有scala选项_Scala中的选项

idea没有scala选项

Scala选项 (Scala options)

The option is a container that contains one single value which can be one of the two distinct values.

选项是一个包含一个值的容器,该值可以是两个不同值之一。

One of the two values is 'none' and others can be any object valid in the program.

这两个值之一是“ none”,其他值可以是程序中有效的任何对象。

The option can be used when accepting values return from a function that can return null the time of period and some value otherwise. the options class returns two Instances:

当接受从函数返回的值时可以使用该选项 ,该函数可以在周期时间返回null,否则返回某个值。 options类返回两个实例:

  1. An instance of a null class, when the function fails.

    当函数失败时,为空类的实例。

  2. An instance of some class, when the function and successfully.

    一个类的实例,当函数成功时。

Both these classes inherit from the option class.

这两个类都从option类继承。

Syntax:

句法:

Declaration of a function that uses option as a return type:

使用option作为返回类型的函数的声明:

    def function_name(arguments) : Option[data_type]

示例展示期权的运作方式 (Example to show how the working of option)

object Demo {
   def details(x: Option[String]) = x match {
      case Some(s) => s
      case None => "?"
   }
   def main(args: Array[String]) {
      val student = Map("name" -> "Ram", "standard" -> "10")
      println("show(student.get( \"name\")) : " + details(student.get( "name")) )
      println("show(student.get( \"percentage\")) : " + details(student.get( "percentage")) )
   }
}

Output

输出量

show(student.get( "name")) : Ram
show(student.get( "percentage")) : ?

选项vs NULL:哪个更好? (Option Vs NULL: which is better?)

The option is compared to NULL in Java programming. Using null in java, for an occasional outcome need it to be handled. If not handled it may give a NullPointerException. While using the Option in scala this exception does not occur that's why its usage is a bit more effective.

选项在Java编程中与NULL比较。 在Java中使用null,偶尔需要处理它。 如果未处理,则可能给出NullPointerException。 在scala中使用Option时,不会发生此异常,这就是其用法更有效的原因。

Scala Options类的一些常用方法 (Some common methods of Scala Options class)

Method Description
def get : AReturns the value of the option.
def isempty : BooleanReturns true for none value and false otherwise.
def getOrElse(val)Returns value for some value in option and return the passed value if none.
def foreach()Evaluates a function if a value exists otherwise nothing is to be done
def flatmap()Returns the value of function for some value of option. If the value does not exists then returns None.
def productElementName(n)Return the element at the n place in 0 based-index.
方法 描述
def get:A 返回选项的值。
def isempty:布尔值 如果没有值,则返回true,否则返回false。
def getOrElse(val) 返回选项中某个值的值,如果没有则返回传递的值。
def foreach() 如果值存在则求值,否则不做任何事情
def flatmap() 返回某个选项值的函数值。 如果该值不存在,则返回None。
def productElementName(n) 返回基于0的索引中位于n位置的元素。

翻译自: https://www.includehelp.com/scala/options-in-scala.aspx

idea没有scala选项

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值