官术网_书友最值得收藏!

  • Learn Scala Programming
  • Slava Schmidt
  • 578字
  • 2021-06-10 19:35:48

Function as a method

Most Scala developers came to it from Java. Because of this, probably the most common way is to define a method inside of a class, trait, or an object, like in the following familiar example:

class MethodDefinition {
def eq(arg1: String, arg2: Int): Boolean = ! nonEqual(arg1, arg2)
private def nonEq(a: String, b: Int) = a != b.toString
}

By convention, we've explicitly defined a return type for the public method in the same way that we would do for the return type in Java. For the non recursive function, the result type can be omitted. We've done this for the private method. 

The type declaration for the value parameters is mandatory.

Each value parameter can have one default value assigned to it:

def defaultValues(a: String = "default")(b: Int = 0, c: String = a)(implicit d: Long = b, e: String = a) = ???

The preceding snippet also demonstrates that it is possible to define multiple groups of value parameters. The parameters from the last group can be implicit and also can be provided with default values. The default values from consecutive groups can refer to the parameters defined in previous groups as e refers to the default value of c, which is a.

The type of the value parameter prefixed with => means that this parameter should not be evaluated at the moment the method is called, but instead each time the parameter is referenced in the body of the method. Such arguments are called by-name parameters and basically, they represent a zero-argument method with the argument's return type:

scala> def byName(int: => Int) = {
| println(int)
| println(int)
| }
byName: (int: => Int)Unit
scala> byName({ println("Calculating"); 10 * 2 })
Calculating
20
Calculating
20

In this example, we can see how the passed block of code is executed twice, matching the number of usages inside the method's body.

The * (star) can be added as a suffix to the name of the type of the last value parameter to denote that this is a repeated parameter and it takes a number of arguments of the defined type. The given arguments are then available in the method body as a collection.Seq of the specified type:

def variable(a: String, b: Int*): Unit = {
val bs: collection.Seq[Int] = b
}

variable("vararg", 1, 2, 3)
variable("Seq", Seq(1, 2, 3): _*)

It is illegal to pass the Seq direct in place of repeated parameters. The last line in the previous snippet shows the :_* syntax to mark the last parameter as a sequence argument. The repeated parameter can't take default values.

Arguments in the method definition have names. These names can be used to call the method by providing arguments in any order (as opposed to the order specified in the definition of the method):

def named(first: Int, second: String, third: Boolean) = s"$first, $second, $third"

named(third = false, first = 10, second = "Nice")
named(10, third = true, second = "Cool")

The named and normal arguments can be mixed, as shown in the last line of the previous code. In this case, the positional arguments must be specified first.

Until now, we defined our examples in the scope of the enclosing class or object. But Scala gives more flexibility in this regard. A method can be defined in any valid scope. This makes it local to the enclosing block and thus limits its visibility. 

主站蜘蛛池模板: 甘泉县| 岗巴县| 兰州市| 花莲县| 曲松县| 长寿区| 临澧县| 江源县| 白河县| 全椒县| 遂宁市| 高安市| 姚安县| 安泽县| 洛川县| 泸水县| 元江| 修武县| 阳朔县| 乌鲁木齐县| 隆昌县| 水富县| 五峰| 建水县| 塘沽区| 罗源县| 保靖县| 贵南县| 军事| 穆棱市| 邯郸市| 加查县| 临沭县| 阿巴嘎旗| 乌拉特前旗| 四子王旗| 鸡泽县| 三台县| 应城市| 乐陵市| 巴青县|