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

I'm new to Scala and I really like it, but sometimes it surprises me. For example:

clickedCallbacks: List[() => Unit]) 

Can someone tell me what => and () => mean in Scala?

+60
scala
Aug 05 2018-11-11T00:
source share
5 answers

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

For example, the type Int => String is equivalent to the type Function1[Int,String] , that is, 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 

Here myInt bound to the argument value passed to f and f2 .

() => T is a type of function that takes no arguments and returns a T This is equivalent to Function0[T] . () is called a list of null parameters, which I consider.

  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 
+91
Aug 05 2018-11-11T00:
source share
— -

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

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

  • In a type with symbols on both sides of the arrow (for example, A => T , (A,B) => T , (A,B,C) => T , etc.) it 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 partners on the left side (for example, () => T ) indicate that the function does not accept any parameters (also sometimes called "thunk");

    • Empty partners on the right side mean that it returns () - the only value of type Unit , whose name can also be written () - :)

      The function returning Unit is also known as a procedure, usually a method that only calls its side effect.

  • In a type declaration for a parameter of a method or function, without a character in the left part (for example, def f(param: => T) ), it is a parameter "by name", which means that it is evaluated every time it is used in the function body but not before. The usual "by value" parameters are evaluated before entering the function / method.

  • In the case clause, they separate the pattern (and additional protection) from the result expression, for example. case x => y .

+73
Aug 05 '11 at 6:17
source share

() => Unit means: "A type function that takes no parameters and returns nothing"

So, if you declare the value of f function that takes no parameters and returns nothing, its type would be as follows:

 val f : () => Unit 

Since this is val , you need to assign a value like function that prints Hola mundo

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

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

Since in Scala you can use type inference that you do not need to declare so that the following is equivalent:

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

To call it, you can simply:

 f() >"Hola mundo" 

Or, since functions are also objects, you can call the apply method:

 f.apply() > "Hola Mundo" 

That's why in your declaration you say: "I will have a list that will contain functions without parameters and return types", therefore List [() => Unit]

Hope this helps.

+13
Aug 05 2018-11-11T00:
source share

=> is the "function arrow". It is used both in function type signatures and in terms of anonymous functions. () => Unit is an abbreviation for Function0[Unit] , which is a type of function that takes no arguments and returns nothing useful (for example, void in other languages).

+5
Aug 05 2018-11-11T00:
source share

As the most simplified answer, you can replace everything that is on the left side => with the word "LEFT" and everything that is on the right with the word "RIGHT".

Then the value " LEFT => RIGHT " becomes:

Take LEFT, then do RIGHT.

This means that if you have "() =>", that you can’t take anything (that is, there are no parameters) and then do whatever is on the right.

This is the most common value.

+3
Mar 17 '15 at 2:46
source share



All Articles