<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Daniel Toni</title>
    <description>The latest articles on DEV Community by Daniel Toni (@portoni).</description>
    <link>https://dev.to/portoni</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3960499%2F31792568-ba6c-47e3-a270-f5d922f76d82.jpg</url>
      <title>DEV Community: Daniel Toni</title>
      <link>https://dev.to/portoni</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/portoni"/>
    <language>en</language>
    <item>
      <title>What Is a Function in Scala</title>
      <dc:creator>Daniel Toni</dc:creator>
      <pubDate>Sun, 31 May 2026 02:41:06 +0000</pubDate>
      <link>https://dev.to/portoni/what-is-a-function-in-scala-1n2b</link>
      <guid>https://dev.to/portoni/what-is-a-function-in-scala-1n2b</guid>
      <description>&lt;p&gt;One of the most important concept in Scala and Functional Programming, is that functions are first-class values. This means functions are treated as any other data type in the language: they can be assigned to variables, passed as arguments to other functions, and returned from functions.&lt;/p&gt;

&lt;p&gt;Treating functions as first-class values enables powerful code design capabilities, such as:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Higher-Order Functions&lt;/li&gt;
&lt;li&gt;Function Composition&lt;/li&gt;
&lt;li&gt;Closures and Currying&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The way Scala achieves this feature is by treating functions as objects, instances of a class, just like &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Int&lt;/code&gt; or any other type.&lt;/p&gt;

&lt;h2&gt;
  
  
  A function is an object with one method.
&lt;/h2&gt;

&lt;p&gt;First, let's look at the long way of building a function value. We can define a generic trait called &lt;code&gt;MyFunction&lt;/code&gt; that takes a parameter of type &lt;code&gt;A&lt;/code&gt; and returns a value of type &lt;code&gt;B&lt;/code&gt;. This trait will expose an &lt;code&gt;apply&lt;/code&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="nc"&gt;MyFunction&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;, &lt;span class="kt"&gt;B&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arg&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;B&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can then create a variable and instantiate an anonymous class using this trait:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;doubler&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MyFunction&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;, &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arg&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arg&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;randomNumber&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;14&lt;/span&gt;

&lt;span class="nv"&gt;doubler&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;apply&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;randomNumber&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 28&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how &lt;code&gt;doubler&lt;/code&gt; is an object with an &lt;code&gt;apply&lt;/code&gt; method. The Scala compiler provides syntactic sugar that allows us to omit the explicit &lt;code&gt;.apply&lt;/code&gt; invocation, resulting in &lt;code&gt;doubler(randomNumber)&lt;/code&gt;. This matches the exact syntax we use to call methods, making every function value look and feel callable.&lt;/p&gt;

&lt;p&gt;Fortunately, we don't need to define these traits ourselves. The Scala standard library provides built-in traits named &lt;code&gt;FunctionX&lt;/code&gt;, such as &lt;code&gt;Function1[A, B]&lt;/code&gt; and &lt;code&gt;Function2[A, B, C]&lt;/code&gt;. The number in the trait name represents how many parameters the function accepts. Thus, our custom &lt;code&gt;MyFunction[A, B]&lt;/code&gt; is functionally equivalent to &lt;code&gt;Function1[A, B]&lt;/code&gt; (though the standard library traits bundle additional useful helper methods).&lt;/p&gt;

&lt;p&gt;To illustrate this with multiple parameters, here is how we create a function that adds two integers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;adder&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Function2&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;, &lt;span class="kt"&gt;Int&lt;/span&gt;, &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;anotherNumber&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;

&lt;span class="nf"&gt;adder&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;randomNumber&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;anotherNumber&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 26&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This demonstrates that every function value under the hood is an instance of a built-in &lt;code&gt;FunctionX&lt;/code&gt; trait.&lt;/p&gt;

&lt;h2&gt;
  
  
  The &lt;code&gt;=&amp;gt;&lt;/code&gt; syntactic sugar for function types
&lt;/h2&gt;

&lt;p&gt;Writing out &lt;code&gt;FunctionX&lt;/code&gt; can become verbose. To make code more concise, the Scala compiler provides syntactic sugar using the &lt;code&gt;=&amp;gt;&lt;/code&gt; operator to define function types:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="nc"&gt;Int&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;                 &lt;span class="c1"&gt;// sugar for Function1[Int, Int]&lt;/span&gt;
&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;          &lt;span class="c1"&gt;// sugar for Function2[Int, Int, Int]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using this notation, our &lt;code&gt;adder&lt;/code&gt; definition becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;adder&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;new&lt;/span&gt; &lt;span class="o"&gt;((&lt;/span&gt;&lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Defining function values with function literals
&lt;/h2&gt;

&lt;p&gt;While the anonymous class syntax works, Scala offers an even cleaner way to produce the exact same object: function literals (also known as lambda expressions).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;doubler&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arg&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;arg&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;

&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;adder&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Though the syntax is completely different, the end result is identical. The compiler abstracts away the boilerplate, instantiating the appropriate &lt;code&gt;FunctionX&lt;/code&gt; trait under the hood and overriding the &lt;code&gt;apply&lt;/code&gt; method with your function's body. We write the logic, and the compiler writes the &lt;code&gt;new ... { override def apply... }&lt;/code&gt; boilerplate for us.&lt;/p&gt;

&lt;h2&gt;
  
  
  Functions as first-class values
&lt;/h2&gt;

&lt;p&gt;Because functions are objects, we can effortlessly pass them as arguments to other functions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="py"&gt;map&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doubler&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;or&lt;/span&gt;

&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="py"&gt;map&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;def&lt;/code&gt; vs &lt;code&gt;val&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;It is important not to mix up two distinct concepts: &lt;code&gt;val&lt;/code&gt; functions and &lt;code&gt;def&lt;/code&gt; methods. While we often call both "functions", they behave differently under the hood:&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;val&lt;/code&gt; holds data, an instance of a &lt;code&gt;FunctionX&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;def&lt;/code&gt; defines a method. A method belongs to a class or object, does not hold data on its own, and is not an instance of &lt;code&gt;FunctionX&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When you pass a method to a place that expects a function value, the compiler automatically converts that method into a function object. This mechanism is called Eta-expansion. (I will discuss in a future post).&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;A function in scala is an object, an instance of a &lt;code&gt;FunctionX&lt;/code&gt; trait whose job is its &lt;code&gt;apply&lt;/code&gt; method. &lt;code&gt;=&amp;gt;&lt;/code&gt; is sugar for the type, &lt;code&gt;f(x)&lt;/code&gt; is sugar for &lt;code&gt;f.aaply(x)&lt;/code&gt;, and a function literal is a concise shorthand the compiler translates into a full function object at runtime&lt;/p&gt;

</description>
      <category>scala</category>
      <category>functional</category>
    </item>
  </channel>
</rss>
