What is a function literal in Scala?

32,369

Solution 1

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

Solution 2

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)
Share:
32,369
prassee
Author by

prassee

java programmer now turned Scala enthusiast

Updated on April 03, 2021

Comments

  • prassee
    prassee over 2 years

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

  • Admin
    Admin almost 13 years
    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).
  • ses
    ses almost 10 years
    def add = (a:Int, b:Int) => a + b Is not the same? I can use this 'add' to pass to another function also
  • fmv1992
    fmv1992 almost 6 years
    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?
  • Mario Galic
    Mario Galic almost 6 years
    @monteiro List(1,2,3).map((x: Int) => { x.toString }: String)
  • AdrianS
    AdrianS almost 6 years
    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 _
  • Abdul Mannan
    Abdul Mannan over 5 years
    Functions can be passed as an argument but methods cannot.