val x: Int=> Int = { y => y }
// ① This is correct
( x1: Int) => Int = { y => y}
// ② This is wrong
I understand a simple scala function like this : x:Int => x
or this : val f =(x: Int) => x
but how to explain the role of "x" in the sentence ①
In val x: Int=> Int = { y => y }
, Int=> Int
is used to define the return type of x
immutable variable which takes Int as input and returns an Int value .
(x1: Int) => Int = { y => y}
is wrong because a function cannot be assigned to another function as (x1: Int) => Int
is a function and { y => y}
is another function
val f =(x: Int) => x
is correct as you are assigning (x: Int) => x
function which takes an integer value as input and returns as it is and is assigned to a immutable variable f.
Defining x in one line would be x is a immutable input variable passed in to a function where manipulation on x is performed .
Here:
val x: Int=> Int = { y => y }
x is a function which receives an integer and returns the same integer.
scala> x(5)
res4: Int = 5
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.