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

Functions and classes

In Scala, every value is an object. Functions are first class values, which also makes them objects of their respective classes. The following figure shows the Scala unified type system and how this is achieved. It is adapted from http://www.scala-lang.org/old/sites/default/files/images/classhierarchy.png and represents an up-to-date view of the model (some classes such as ScalaObject have disappeared, as we have already mentioned before).

As you can see, Scala does not have the same concept of primitive types that Java has and all types are ultimately subtypes of Any.

Functions as classes

The fact that functions are classes means that they can be freely passed to other methods or classes as if they were just values. This leads to improving the expressiveness of Scala and making it much easier to achieve things, such as callbacks, than in other languages such as Java.

Function literals

Let's have a look at an example:

class FunctionLiterals { 
  val sum = (a: Int, b: Int) => a + b 
} 

object FunctionLiterals { 
  
  def main(args: Array[String]): Unit = { 
    val obj = new FunctionLiterals 
    System.out.println(s"3 + 9 = ${obj.sum(3, 9)}") 
  } 
}

Here we can see how the sum field of the FunctionLiterals class is actually assigned a function. We can assign any function to a variable and then call it as if it was a function (essentially invoking its apply method). Functions can also be passed as parameters to other methods. Let's add the following to our FunctionLiterals class:

def runOperation(f: (Int, Int) => Int, a: Int, b: Int): Int = { 
  f(a, b) 
}

We can then pass the required function to runOperation, as follows:

obj.runOperation(obj.sum, 10, 20)
obj.runOperation(Math.max, 10, 20)
Functions without syntactic sugar

In the preceding example, we just used syntactic sugar. In order to understand exactly what happens, we will show to what the function literals are converted. They basically represent extensions to the FunctionN trait where N is the number of parameters. The implementations of the literals are invoked using the apply method (whenever a class or an object has an apply method, it can be implicitly invoked just using parentheses after the object name or instance and passing the required parameters, if any). Let's see the equivalent implementation to our previous example:

class SumFunction extends Function2[Int, Int, Int] { 
  override def apply(v1: Int, v2: Int): Int = v1 + v2 
} 

class FunctionObjects { 
  val sum = new SumFunction 
  
  def runOperation(f: (Int, Int) => Int, a: Int, b: Int): Int = 
    f(a, b) 
} 

object FunctionObjects { 
  
  def main(args: Array[String]): Unit = { 
    val obj = new FunctionObjects 
    System.out.println(s"3 + 9 = ${obj.sum(3, 9)}") 
    System.out.println(s"Calling run operation: ${obj. runOperation(obj.sum, 10, 20)}") 
    System.out.println(s"Using Math.max: ${obj.runOperation(Math.max, 10, 20)}") 
  } 
}

Increased expressivity

As you can see from the examples, unifying classes and functions leads to increased expressivity and we can easily achieve various things such as callbacks, lazy parameter evaluation, centralized exception handling, and others and without writing extra code and logic. Moreover, functions as classes mean that we can extend them to provide extra functionality.

主站蜘蛛池模板: 六枝特区| 蓬安县| 丰镇市| 藁城市| 五河县| 左贡县| 竹北市| 巴林右旗| 大新县| 望都县| 肇源县| 绥棱县| 马鞍山市| 海南省| 苍溪县| 深圳市| 深圳市| 阳谷县| 资中县| 天全县| 淮安市| 双辽市| 罗城| 盐津县| 洪湖市| 分宜县| 报价| 望谟县| 滕州市| 绥化市| 乌恰县| 射洪县| 东乡县| 炎陵县| 佛教| 乐至县| 黄骅市| 丁青县| 民权县| 天水市| 五华县|