This question already has an answer here:
这个问题已经有了答案:
- What's the difference between => , ()=>, and Unit=> 2 answers
- =>,()=>,和Unit=> 2答案!
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里是什么意思?
5 个解决方案
#1
97
=>
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 Int
and returns a String
.
例如,type Int =>字符串,相当于类型Function1[Int,String],即一个函数,它接受Int类型的参数并返回一个字符串。
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 myInt
is binded to the argument value passed to f
and f2
.
这里,myInt被绑定到传递给f和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.
()=> T是一个不带参数,返回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
#2
74
=>
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 }
在一个值中,它引入了一个函数文字,或者lambda。列表中的大括号中的位(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 forFunction<n>[A[,B,...],T]
, that is, a function that takes parameters of typeA[,B...]
, and returns a value of typeT
.在一种类型中,箭头的两边都有符号(例如:a => T, (a,B) => T, (a,B,C) => T,等等),它是糖的作用,
[a,B,…[参考译文]这是一个函数,它接受a类型的参数[,B…,并返回T类型的值。 -
Empty parens on the left hand side (e.g.
() => T
) indicate that the function takes no parameters (also sometimes called a "thunk");左侧的空parens(例如,()=> T)表示该函数不接受任何参数(有时也称为“thunk”);
-
Empty parens on the right hand side denote that it returns
()
—the sole value of typeUnit
, whose name can also be written()
—confused yet? :)右侧的空parens表示它返回()——类型单元的唯一值,它的名称也可以被写()-混淆了吗?:)
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.在方法或函数参数的类型声明中,在左侧没有符号(例如,def f(param: => T)),它是一个“by name参数”,意思是每次在函数的主体中使用时都进行评估,而不是在以前。在进入函数/方法之前,对普通的“逐值”参数进行评估。
-
In a
case
clause, they separate the pattern (and optional guard) from the result expression, e.g.case x => y
.在一个case子句中,他们将模式(和可选的保护)从结果表达式中分离出来,例如,case x => y。
#3
14
() => Unit
means: "Type function that takes no parameters and return nothing"
()=>单元的意思是:“类型函数,不带参数,什么也不返回”
So if you declare a value f
to be a function that takes no parameters and returns nothing its type would be:
因此,如果你声明一个值f为一个函数,它不接受参数,也不返回它的类型:
val f : () => Unit
Since this is a val
you 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 apply
method:
或者,由于函数也是对象,您可以调用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]
这就是为什么在您的声明中,您会说“我将有一个列表,它将包含没有参数的函数,并且没有返回类型”因此列表[()=>单元]
I hope this helps.
我希望这可以帮助。
#4
5
=>
is the "function arrow". It is used both in function type signatures as well as anonymous function terms. () => Unit
is a shorthand for Function0[Unit]
, which is the type of functions which take no arguments and return nothing useful (like void
in other languages).
=>是“函数箭头”。它既用于函数类型签名,也用于匿名函数。()=>单元是Function0[Unit]的简写,它是不带参数的函数类型,返回无用的函数(如其他语言中的void)。
#5
4
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.
这是最普遍的意思。
#1
97
=>
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 Int
and returns a String
.
例如,type Int =>字符串,相当于类型Function1[Int,String],即一个函数,它接受Int类型的参数并返回一个字符串。
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 myInt
is binded to the argument value passed to f
and f2
.
这里,myInt被绑定到传递给f和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.
()=> T是一个不带参数,返回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
#2
74
=>
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 }
在一个值中,它引入了一个函数文字,或者lambda。列表中的大括号中的位(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 forFunction<n>[A[,B,...],T]
, that is, a function that takes parameters of typeA[,B...]
, and returns a value of typeT
.在一种类型中,箭头的两边都有符号(例如:a => T, (a,B) => T, (a,B,C) => T,等等),它是糖的作用,
[a,B,…[参考译文]这是一个函数,它接受a类型的参数[,B…,并返回T类型的值。 -
Empty parens on the left hand side (e.g.
() => T
) indicate that the function takes no parameters (also sometimes called a "thunk");左侧的空parens(例如,()=> T)表示该函数不接受任何参数(有时也称为“thunk”);
-
Empty parens on the right hand side denote that it returns
()
—the sole value of typeUnit
, whose name can also be written()
—confused yet? :)右侧的空parens表示它返回()——类型单元的唯一值,它的名称也可以被写()-混淆了吗?:)
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.在方法或函数参数的类型声明中,在左侧没有符号(例如,def f(param: => T)),它是一个“by name参数”,意思是每次在函数的主体中使用时都进行评估,而不是在以前。在进入函数/方法之前,对普通的“逐值”参数进行评估。
-
In a
case
clause, they separate the pattern (and optional guard) from the result expression, e.g.case x => y
.在一个case子句中,他们将模式(和可选的保护)从结果表达式中分离出来,例如,case x => y。
#3
14
() => Unit
means: "Type function that takes no parameters and return nothing"
()=>单元的意思是:“类型函数,不带参数,什么也不返回”
So if you declare a value f
to be a function that takes no parameters and returns nothing its type would be:
因此,如果你声明一个值f为一个函数,它不接受参数,也不返回它的类型:
val f : () => Unit
Since this is a val
you 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 apply
method:
或者,由于函数也是对象,您可以调用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]
这就是为什么在您的声明中,您会说“我将有一个列表,它将包含没有参数的函数,并且没有返回类型”因此列表[()=>单元]
I hope this helps.
我希望这可以帮助。
#4
5
=>
is the "function arrow". It is used both in function type signatures as well as anonymous function terms. () => Unit
is a shorthand for Function0[Unit]
, which is the type of functions which take no arguments and return nothing useful (like void
in other languages).
=>是“函数箭头”。它既用于函数类型签名,也用于匿名函数。()=>单元是Function0[Unit]的简写,它是不带参数的函数类型,返回无用的函数(如其他语言中的void)。
#5
4
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.
这是最普遍的意思。