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中何時使用下劃線