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类返回两个实例:
An instance of a null class, when the function fails.
当函数失败时,为空类的实例。
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 : A | Returns the value of the option. |
def isempty : Boolean | Returns 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选项