scala中的=>符号的含义

Posted SHIHUC

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了scala中的=>符号的含义相关的知识,希望对你有一定的参考价值。

【声明】本帖的内容是copy来的,来源为stack overflow。

 

   It has several meanings in Scala, all related to its mathematical meaning as implication.

  • In a value, it introduces a function literal, or lambda. e.g. the bit inside the curly braces in List(1,2,3).map { (x: Int) => x * 2 }

  • In a type, with symbols on both sides of the arrow (e.g. A => T, (A,B) => T, (A,B,C) => T, etc.) it‘s sugar for Function<n>[A[,B,...],T], that is, a function that takes parameters of type A[,B...], and returns a value of type T.

    • Empty parens on the left hand side (e.g. () => T) indicate that the function takes no parameters (also sometimes called a "thunk");

    • Empty parens on the right hand side denote that it returns ()—the sole value of type Unit, whose name can also be written ()—confused yet? :)

      A function that returns Unit is also known as a procedure, normally a method that‘s called only for its side effect.

  • In the type declaration for a method or function parameter, with no symbol on the left hand side (e.g. def f(param: => T)) it‘s a "by-name parameter", meaning that is evaluated every time it‘s used within the body of the function, and not before. Ordinary "by-value" parameters are evaluated before entry into the function/method.

  • In a case clause, they separate the pattern (and optional guard) from the result expression, e.g. case x => y.

 

=> is syntactic sugar for creating instances of functions. Recall that every function in scala is an instance of a class.

For example, the type Int => String, is equivalent to the type Function1[Int,String] i.e. a function that takes an argument of type Int and returns a String.  scala> val f: Function1[Int,String] = myInt => "my int: "+myInt.toString

  f: (Int) => String = <function1>

  scala> f(0)
  res0: String = my int: 0

  scala> val f2: Int => String = myInt => "my int v2: "+myInt.toString
  f2: (Int) => String = <function1>

  scala> f2(1)
  res1: String = my int v2: 1

 
scala> val f2: Function2[Int,Int,String] = (myInt1,myInt2) => "This is my function to transfer " + myInt1 + " and " + myInt2 + " as a string component."
 f2: (Int, Int) => String = <function2>

 scala> f2(1,2)

  res6: String = This is my function to transfer 1 and 2 as a string component.

 

  scala> val f22:(Int,Int)=>String = (myInt1,myInt2) => "This is my function to transfer " + myInt1 + " and " + myInt2 + " as a string component."
  f22: (Int, Int) => String = <function2>

  scala> f22(2,4)
  res7: String = This is my function to transfer 2 and 4 as a string component.

 

Here myInt is binded to the argument value passed to f and f2.

() => T is the type of a function that takes no arguments and returns a T. It is equivalent to Function0[T]. () is called a zero parameter list I believe.

 scala> val f: () => Unit = () => { println("x")}
 f: () => Unit = <function0>

 scala> f()
 x

scala> val f2: Function0[Unit] = () => println("x2")
f: () => Unit = <function0>

scala> f2()
x2

 

 

As the most simplified answer, you can substitute whatever is on the left-hand side of => with the word "LEFT" and whatever is on the right-hand side with the word "RIGHT".

Then, the meaning of "LEFT => RIGHT" becomes:

Take LEFT then do RIGHT.

This means that if you have a "()=>" that you can take nothing (that is, no parameters) and then do whatever is on the right-hand side.

This is the most common meaning.

 

从而可以看出,这个符号主要是用在函数(匿名)的定义中。慢慢体会。这个是和java等其他语言有较大差别的地方。。。

以上是关于scala中的=>符号的含义的主要内容,如果未能解决你的问题,请参考以下文章

scala中常用但不常见(其他语言)的符号含义

scala中常用但其他语言不常见的符号含义 - 心灵空谷幽兰 - 博客园

<??> 符号在 C#.NET 中的含义是啥? [复制]

Scala 无法解决符号退出

Scala类参数化中附加冒号的含义

linux打开终端如何启动scala,如何在终端下运行Scala代码片段?