53

What is a function literal in Scala and when should I use them?

Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
prassee
  • 3,651
  • 6
  • 30
  • 49

3 Answers3

84

A function literal is an alternate syntax for defining a function. It's useful for when you want to pass a function as an argument to a method (especially a higher-order one like a fold or a filter operation) but you don't want to define a separate function. Function literals are anonymous -- they don't have a name by default, but you can give them a name by binding them to a variable. A function literal is defined like so:

(a:Int, b:Int) => a + b

You can bind them to variables:

val add = (a:Int, b:Int) => a + b
add(1, 2) // Result is 3

Like I said before, function literals are useful for passing as arguments to higher-order functions. They're also useful for defining one-liners or helper functions nested within other functions.

A Tour of Scala gives a pretty good reference for function literals (they call them anonymous functions).

Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
  • 15
    I think it might be useful to point out how a function literal is just sugar over FunctionN -- which would give a generally good case for the literal vs. the "long" version. Also, while I have a +1, it is generally not "alternate syntax for defining a function [did you mean method????]" as ***functions are not methods*** -- methods are construct that *only* apply to classes and messages which can be dispatched to them (the classes). *functions* are "first class values" (objects that support `apply` and a few others). –  Mar 09 '11 at 04:21
  • def add = (a:Int, b:Int) => a + b Is not the same? I can use this 'add' to pass to another function also – ses Mar 01 '14 at 18:07
  • you can also create a function literal from an existing function with the underscope operator: def addTwoNumbers(a: Int, b: Int) = a + b; def add2 = addTwoNumbers _ – AdrianS Feb 01 '18 at 08:05
  • 1
    Functions can be passed as an argument but methods cannot. – Abdul Mannan Sep 09 '18 at 06:15
21

It might be useful to compare function literals to other kinds of literals in Scala. Literals are notational sugar for representing values of some types the language considers particularly important. Scala has integer literals, character literals, string literals, etc. Scala treats functions as first class values representable in source code by function literals. These function values inhabit a special function type. For example,

  • 5 is an integer literal representing a value in Int type
  • 'a' is a character literal representing a value in Char type
  • (x: Int) => x + 2 is a function literal representing a value in Int => Int function type

Literals are often used as anonymous values, that is, without bounding them to a named variable first. This helps make the program more concise and is appropriate when the literal is not meant to be reusable. For example:

List(1,2,3).filter((x: Int) => x > 2)

vs.

val one: Int = 1
val two: Int = 2
val three: Int = 3
val greaterThan2: Int => Boolean = (x: Int) => x > two
List(one,two,three).filter(greaterThan2)
Mario Galic
  • 47,285
  • 6
  • 56
  • 98
  • 1
    Is there a way to make the types more explicit? This works: `List(1,2,3).map(x => x + 10)` This does not work: `List(1,2,3).map((x: Int) : Str => x.toString)` This works but it is not so explicit: `List(1,2,3).map((x: Int) => x.toString)` Or if I wanted to do this I should use a named function? – fmv1992 Jan 21 '18 at 19:16
  • 3
    @monteiro `List(1,2,3).map((x: Int) => { x.toString }: String)` – Mario Galic Jan 22 '18 at 11:33
0

Programming in Scala, Third Edition

8.3 FIRST-CLASS FUNCTIONS

A function literal is compiled into a class that when instantiated at runtime is a function value.[2] Thus the distinction between function literals and values is that function literals exist in the source code, whereas function values exist as objects at runtime. The distinction is much like that between classes (source code) and objects (runtime).

mon
  • 18,789
  • 22
  • 112
  • 205