=> 和 () => 在 Scala 中是什么意思

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6951895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 03:19:00  来源:igfitidea点击:

What does => and () => mean in Scala

scala

提问by ducktyped

I am new to Scala and I really like it, but sometimes it surprises me. For instance:

我是 Scala 的新手,我真的很喜欢它,但有时它让我感到惊讶。例如:

clickedCallbacks: List[() => Unit])

Could anyone tell me what =>and () =>mean in Scala?

谁能告诉我Scala 中的含义=>() =>含义?

回答by Carlos López-Camey

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

=>是用于创建函数实例的语法糖。回想一下 scala 中的每个函数都是一个类的实例。

For example, the type Int => String, is equivalent to the type Function1[Int,String]i.e. a function that takes an argument of type Intand returns a String.

例如,类型Int => String, 等价于类型,Function1[Int,String]即接受类型参数Int并返回 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

Here myIntis bound to the argument value passed to fand f2.

HeremyInt绑定到传递给fand的参数值f2

() => Tis 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.

() => T是不带参数并返回 a 的函数的类型T。它相当于Function0[T]()我相信被称为零参数列表。

 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

回答by Alex Cruise

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

=>在 Scala 中有多种含义,都与其作为蕴涵的数学含义有关。

  • 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 caseclause, they separate the pattern (and optional guard) from the result expression, e.g. case x => y.

  • 在一个值中,它引入了一个函数文字或lambda。例如花括号里面的位List(1,2,3).map { (x: Int) => x * 2 }

  • 在一个类型中,箭头两边都有符号(例如A => T, (A,B) => T,(A,B,C) => T等)它是 for 的糖Function<n>[A[,B,...],T],也就是说,一个函数接受 type 的参数A[,B...],并返回一个 type 的值T

    • 左侧的空括号(例如() => T)表示该函数不接受参数(有时也称为“thunk”);

    • 右边的空括号表示它返回()—— type 的唯一值Unit,它的名字也可以写——()混淆了吗?:)

      返回 Unit 的函数也称为过程,通常是仅因其副作用而调用的方法。

  • 在方法或函数参数的类型声明中,左侧没有符号(例如def f(param: => T))它是一个“按名称参数”,这意味着每次在函数体内使用时都会对其进行评估,而不是之前。普通的“按值”参数在进入函数/方法之前被评估。

  • case子句中,它们将模式(和可选的保护)与结果表达式分开,例如case x => y.

回答by OscarRyz

() => Unitmeans: "Type function that takes no parameters and return nothing"

() => Unit意思是:“类型函数,不带参数,不返回任何内容”

So if you declare a value fto be a function that takes no parameters and returns nothing its type would be:

因此,如果您将值声明为f不带参数且不返回任何内容的函数,则其类型将是:

val f : () => Unit

Since this is a valyou have to assign a value, for instance a function that prints Hola mundo

由于这是一个val你必须分配一个值,例如一个打印的函数Hola mundo

val f : () => Unit  = () => println("Hola Mundo")

That reads: *"f is a function that takes no parameters and returns nothing initialized with the code println("Hola Mundo")

内容如下: *"f 是一个不带参数且不返回任何用代码初始化的函数 println("Hola Mundo")

Since in Scala you can use type inference you don't have to declare the type so the following would be equivalent:

由于在 Scala 中您可以使用类型推断,因此您不必声明类型,因此以下内容是等效的:

val f = () => println("Hola Mundo")

To invoke it you can just:

要调用它,您只需:

f()

>"Hola mundo"

Or, since the functions are also objects you can invoke the applymethod:

或者,由于函数也是对象,您可以调用该apply方法:

f.apply()
> "Hola Mundo"

That's why in your declaration you're saying "I'll have a list that will hold functions with no params and no return types"hence List[()=>Unit]

这就是为什么在你的声明中你说“我将有一个列表,该列表将保存没有参数和返回类型的函数”,因此 List[()=>Unit]

I hope this helps.

我希望这有帮助。

回答by Damir Olejar

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".

作为最简单的答案,您可以将 => 左侧的任何内容替换为“LEFT”一词,将右侧的任何内容替换为“RIGHT”。

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

那么,“ LEFT => RIGHT”的意思就变成了:

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.

这是最常见的意思。

回答by Tom Crockett

=>is the "function arrow". It is used both in function type signatures as well as anonymous function terms. () => Unitis a shorthand for Function0[Unit], which is the type of functions which take no arguments and return nothing useful (like voidin other languages).

=>是“功能箭头”。它用于函数类型签名以及匿名函数术语。() => Unit是 的简写Function0[Unit],它是一种不带参数且不返回任何有用信息的函数类型(就像void在其他语言中一样)。