"Statement" The content of this post is copy, the source is the stack overflow.
It has several meanings in Scala and 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 inList(1,2,3).map { (x: Int) => x * 2 }
-
In a type, with symbols on both sides of the arrow (e.g. a = t
, (b) + t
, (a,b,c) = + T
, etc.) It's sugar for function<n>[a[,b,...],t]
, that's, 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 parame Ters (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 the Unit is also known as A procedure , normally A method that's called only Side effect.
In the type declaration-a method or function parameter, with no symbol on the left hand side (e.g. def f(param: => T)
) It's a "by-n Ame parameter ", meaning that's evaluated every time it ' s used within the body of the function, and not before. Ordinary "By-value" parameters is 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 are an instance of a class.
For example, the type Int => String
, was equivalent to the type Function1[Int,String]
i.e. a function that takes an argument of type and Int
Retu RNs a String
. scala> val f: Function1[Int,String] = myInt => "my int: "+myInt.toString
F: (Int) = String = <Function1>Scala>F(0)Res0: String =my int: 0Scala> ValF2: Int = String =MyInt= "My int v2:"+myint.: (int => string= <function1>< Span class= "PLN" > 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) and String = <function2>
scala> F2 ()
Res6:string = This is the My function to transfer 1 and 2 as a String component.
Scala> val f22: (int,int) =>string = (Myint1,myint2) = "This was my function to transfer" + MyInt1 + "and" + MyI Nt2 + "as a string component."
F22: (int, int) and String = <function2>
Scala> F22 (2,4)
Res7:string = This is the My function to transfer 2 and 4 as a String component.
Here's binded to the myInt
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> ValF: () = Unit = () = {println("X")}F: () = Unit = <Function0>Scala>F()Xscala> Val F2: function0[unit] = () => Println ( "X2" Span class= "pun" >) f: () => unit = <function0>> F2 () x2
As the most simplified answer, you can substitute whatever are on the left-hand side of ' with the word ' left ' and what Ever is on the right-hand side with the word ' right '.
Then, the meaning of ' left=Right ' becomes:
Take the left and do right.
This means if you have a "() =" That's You can take nothing (that's, no parameters) and then do whatever are on the Right-hand side.
The most common meaning.
As you can see, this symbol is mainly used in the definition of function (anonymous). Slowly experience. This is a big difference from other languages like Java ...
Meaning of the = = symbol in Scala