scala中何时使用下划线
Underscore (_) character is reserved in Scala and has multiple usages in the programming language. Based on functions that use the underscore have the following usages:
下划线(_)字符在Scala中保留,并且在编程语言中有多种用法。 基于使用下划线的函数具有以下用法 :
1) Existential Types
1)存在类型
These types are the ways of abstracting over data types,
这些类型是抽象数据类型的方法,
def functn(l: List[Option[_]]) = {
}
2) Higher kind type parameters
2)更高种类的参数
A higher kind type is a constructor that contains a type constructor itself. Sample,
更高类型的类型是一个包含类型构造函数本身的构造函数。 样品,
class incl [u[ _ ]]
3) Ignored variables
3)忽略变量
The ignored variables are declared using _. For example,
被忽略的变量使用_声明。 例如,
val _ = 67
4) Ignored names of self-types
4)忽略自我类型的名称
Merging two traits without extending each other is self type. Instead of names of self types, programmers can use the ignored names. Sample,
融合两个特质而不互相延伸是自我类型。 程序员可以使用忽略的名称代替自身类型的名称。 样品,
trait incl { _: seq[_] => }
5) Wildcard patterns
5)通配符模式
In pattern matching, a wildcard pattern is used to match the unmatched case. Sample,
在模式匹配中,通配符模式用于匹配不匹配的大小写。 样品,
case (_) : //code
6) Wildcard imports
6)通配符导入
To import all classes of a package, the wildcard import is used. Sample,
要导入包的所有类,请使用通配符导入。 样品,
import java.util._
7) Joining operators to letter
7)加入运营商来信
Sample,
样品,
def fn_! (x : float) = 5
8) Assignment operator
8)赋值运算符
An assignment operator is an operator that assigns a value to the given variable/method. For example,
赋值运算符是将值分配给给定变量/方法的运算符。 例如,
def include_ = { ... }
9) Placeholder syntax
9)占位符语法
A placeholder is an anonymous function. Sample,
占位符是一个匿名函数。 样品,
list (a, b, c) map(_+c)
10) Method values
10)方法值
If a method returns a single value and program directly uses it, then _ can be used instead of its name. For example, iterating over a list,
如果方法返回单个值并且程序直接使用它,则可以使用_代替其名称。 例如,遍历列表,
list (a, b, c) foreach println _
11) Default initializers
11)默认初始化
The initialization of variables without using values is done using underscore _. For example,
不使用值的变量初始化使用下划线_完成。 例如,
var a : Int = _ // value is 0 i.e. default value.
12) Hiding imports
12)隐藏进口
You can hide imports of scala methods is done using the wildcard _.
您可以使用通配符_来隐藏scala方法的导入。
import java.util.{ArrayList => _, _}
翻译自: https://www.includehelp.com/scala/use-of-underscore-in-scala.aspx
scala中何时使用下划线