文章目录
  1. 1. 下划线
    1. 1.1. Placeholder syntax
    2. 1.2. Wildcard patterns
    3. 1.3. Wildcard imports
    4. 1.4. 类的setter方法
    5. 1.5. Converting call-by-name parameters to functions

下划线

Placeholder syntax

这个用法比较多,表示某一个参数。比如对collection调用方法map、filter、sortWith、foreach等等表示对每一个元素进行处理,甚至可以使用_.XXX方式

Scala represents anonymous functions with a elegant syntax. The _acts as a placeholder for parameters in the anonymous function. The _ should be used only once, But we can use two or more underscores to refer different parameters.

1
List(1,2,3,4,5).foreach(print(_))

1
List(1,2,3,4,5).foreach( a => print(a))

Here the _ refers to the parameter. The first one is a short form of the second one. Lets look at another example which take two parameters.

1
val sum = List(1,2,3,4,5).reduceLeft(_+_)
1
val sum = List(1,2,3,4,5).reduceLeft((a, b) => a + b)

There is a good post which explains the inner details of the above example.

Wildcard patterns

In Scala, pattern matching is somewhat similar to java switch statement. But it is more powerful.

1
2
3
4
5
def matchTest(x: Int): String = x match {
case 1 => "one"
case 2 => "two"
case _ => "anything other than one and two"
}

In Scala, each selector will be matched with the patterns in the order they appear and the first match will be executed. _acts like a wildcard. It will match anything. Scala allows nested patterns, so we can nest the _also.Lets see another example that uses_ in nested pattern.

1
2
3
4
5
6
expr match {
case List(1,_,_) => " a list with three element and the first element is 1"
case List(_*) => " a list with zero or more elements "
case Map[_,_] => " matches a map with any key type and any value type "
case _ =>
}

Wildcard imports

In scala, _acts similar to * in java while importing packages.

1
2
3
4
5
6
7
8
// imports all the classes in the package matching
import scala.util.matching._
// imports all the members of the object Fun. (static import in java)
import com.test.Fun._
// imports all the members of the object Fun but renames Foo to Bar
import com.test.Fun.{ Foo => Bar , _ }
// imports all the members except Foo. To exclude a member rename it to _
import com.test.Fun.{ Foo => _ , _ }

类的setter方法

getters-and-setters-in-scala

In scala, a getter and setter will be implicitly defined for all non-private var in a object. The getter name is same as the variable name and _= is added for setter name. We can define our own getters and setters. Ok lets see an example which uses the getter and setters.

1
2
3
4
5
6
7
8
class Test {
private var a = 0
def age = a
def age_=(n:Int) = {
require(n>0)
a = n
}
}

1
2
3
val t = new Test
t.age = 5
println(t.age)

Converting call-by-name parameters to functions

Scala is a functional language. So we can treat function as a normal variable. If you try to assign a function to a new variable, the function will be invoked and the result will be assigned to the variable. This confusion occurs due to the optional braces for method invocation. We should use _ after the function name to assign it to another variable.

1
2
3
4
5
6
class Test {
def fun = {
// some code
}
val funLike = fun _
}

用于将方法转换成函数


参考资料:

  1. ananthakumaran.in
  2. zhihu
  3. stackoverflow
文章目录
  1. 1. 下划线
    1. 1.1. Placeholder syntax
    2. 1.2. Wildcard patterns
    3. 1.3. Wildcard imports
    4. 1.4. 类的setter方法
    5. 1.5. Converting call-by-name parameters to functions