Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does => and () => mean in Scala [duplicate]

Tags:

scala

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

clickedCallbacks: List[() => Unit]) 

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

like image 441
ducktyped Avatar asked Aug 05 '11 05:08

ducktyped


People also ask

What does => mean in Scala?

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

What does => int mean in Scala?

It means when you call the method, the argument is not evaluated before the method is executed, but rather, it is evaluated each time it is referred to inside the method.

What does arrow mean in Scala?

Arrow is a type class for modeling composable relationships between two types. One example of such a composable relationship is function A => B ; other examples include cats. data. Kleisli (wrapping an A => F[B] , also known as ReaderT ), and cats.

What is higher order function in Scala?

In Scala, a higher-order function is a function which takes another function as an argument. A higher-order function describes "how" the work is to be done in a collection. Let's learn the higher order function map. The map applies the function to each value in the collection and returns a new collection.


2 Answers

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

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.

  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 bound to the 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> val f: () => Unit = () => { println("x")}  f: () => Unit = <function0>   scala> f()  x  scala> val f2: Function0[Unit] = () => println("x2") f: () => Unit = <function0>  scala> f2() x2 
like image 195
Carlos López-Camey Avatar answered Sep 28 '22 04:09

Carlos López-Camey


=> has several meanings in Scala, 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 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 case clause, they separate the pattern (and optional guard) from the result expression, e.g. case x => y.

like image 36
Alex Cruise Avatar answered Sep 28 '22 04:09

Alex Cruise