<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Scala 3 - Medium]]></title>
        <description><![CDATA[A series of posts on Scala version 3, what’s new and why, and how to use its new features effectively. For more details, visit http://programming-scala.org/. - Medium]]></description>
        <link>https://medium.com/scala-3?source=rss----de7527e0301e---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Scala 3 - Medium</title>
            <link>https://medium.com/scala-3?source=rss----de7527e0301e---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 11:44:07 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/scala-3" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Scala 3: What Is “Direct Style”?]]></title>
            <link>https://medium.com/scala-3/scala-3-what-is-direct-style-d9c1bcb1f810?source=rss----de7527e0301e---4</link>
            <guid isPermaLink="false">https://medium.com/p/d9c1bcb1f810</guid>
            <category><![CDATA[scala-programming]]></category>
            <category><![CDATA[scala-3]]></category>
            <dc:creator><![CDATA[Dean Wampler]]></dc:creator>
            <pubDate>Sun, 04 Jun 2023 20:43:03 GMT</pubDate>
            <atom:updated>2023-06-04T20:43:03.502Z</atom:updated>
            <content:encoded><![CDATA[<p>Scala uses <em>monads</em> extensively for many operations with non-trivial <em>effects</em>, such as those involving asynchronous computation, including I/O. The idea of <strong>direct style</strong> is to allow writing code with non-trivial <em>effects</em> more simply without the boilerplate of monads.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VR3QFEHDkXwvLGuENumM1g.jpeg" /><figcaption><a href="https://www.flickr.com/photos/deanwampler/52724610572/in/dateposted-public/">Vista Tower, Chicago</a>, © 2023, Dean Wampler</figcaption></figure><blockquote>I recommend viewing <a href="https://www.youtube.com/watch?v=0Fm0y4K4YO8">this Martin Odersky talk</a> about the on-going work on direct style. The following discussion is based on it.</blockquote><h3>Motivation</h3><p>Consider the example of Futures. We currently use them like this:</p><pre>val sum =<br>  val f1 = Future(c1.read)<br>  val f2 = Future(c2.read)<br>  for<br>    x &lt;- f1<br>    y &lt;- f2<br>  yield x + y</pre><p>The for comprehension invokes map and flatMap to wait on the futures and extract the values.</p><p>A new <strong>direct style</strong> implementation of futures would be used liked this:</p><pre>  val sum = Future:<br>    val f1 = Future(c1.read)<br>    val f2 = Future(c2.read)<br>    f1.value + f2.value</pre><p>In both cases, the two futures are executing in parallel, which might take a while. The sum of the returned values is returned in a new future. The value method returns the result of a future once it is available or it throws an exception if the future returns a Failure. However, an implementation based on the boundary and break mechanism discussed below, this exception would be caught by the Future library, used to cancel the other running Future, if one is still running, and then return a Failure future for sum.</p><p>So, direct style simplifies code, making it is easier to write and understand, plus it enables cleaner separation of concerns, such as handling timeouts and failures in futures, and it cleanly supports composability, which monads don’t provide unless you use cumbersome machinary such as <em>monad transformers</em>.</p><h3>Implementing Direct Style in Scala</h3><p>In Martin’s talk, he discusses four aspects of building support for direct style in Scala:</p><ol><li>boundary and break — now available in Scala 3.3.0.</li><li>Error handling — enabled, but not yet used in the library, which is still the Scala 2.13 library.</li><li>Suspensions — work in progress.</li><li>Concurrent library design built on the above — work in progress.</li></ol><p>Let’s discuss these topics.</p><h3>boundary and break</h3><p>This mechanism is defined with a new addition to the API, <a href="https://www.scala-lang.org/api/3.3.0/scala/util/boundary$.html">scala.util.boundary$</a>. It provides a cleaner alternative to non-local returns.</p><ul><li>boundary defines a context for a computation.</li><li>break returns a value from within the enclosing boundary.</li></ul><p>Here is an example from Martin’s talk, which is also discussed in the <a href="https://www.scala-lang.org/api/3.3.0/scala/util/boundary$.html">API</a>. I have added this example and some tests in the <em>Programming Scala </em>code examples <a href="https://github.com/deanwampler/programming-scala-book-code-examples/tree/master/src/main/scala/progscala3/concurrency/direct">here</a> and <a href="https://github.com/deanwampler/programming-scala-book-code-examples/blob/master/src/test/scala/progscala3/concurrency/direct/BoundaryExamplesSuite.scala">here</a>.</p><pre>import scala.util.boundary, boundary.break<br><br>def firstIndex[T](xs: List[T], elem: T): Int =<br>  boundary:<br>    for (x, i) &lt;- xs.zipWithIndex do<br>      if x == elem then break(i)<br>    -1</pre><p>This is one way to return the index for an element found in a collection or -1 if not found. The boundary defines the scope where a non-local return may be invoked. If the desired element elem is found, then we call break to return the index. This breaks out of the for comprehension, too, since there is no point in continuing. If elem isn’t found, -1 is returned through the normal return path.</p><p>Here is a slightly simplified implementation. (The full 3.3.0 source code is <a href="https://github.com/lampepfl/dotty/blob/3.3.0/library/src/scala/util/boundary.scala">here</a>):</p><pre>package scala.util<br><br>object boundary:<br>  final class Label[-T]<br><br>  inline def apply[T](inline body: Label[T] ?=&gt; T): T = ...<br><br>  def break[T](value: T)(using label: Label[T]): Nothing =<br>    throw Break(label, value)<br><br>  final class Break[T] (val label: Label[T], val value: T) extends RuntimeException(...)<br><br>end boundary</pre><p>In the firstIndex example above, the boundary: line is short for boundary.apply: with the indented code below it passed as the body.</p><p>Well actually, the block passed to boundary.apply is a <em>context function</em> that is called within boundary.apply to return the block of code shown in the example. Note the final class Label[T] declaration in boundary. Users don’t define Label instances themselves. Instead, this is done inside the implementation (not shown) of boundary.apply to provide the <em>capability</em> of doing a non-local return. Using a Label in this way prevents the user from trying to call break without an enclosing boundary.</p><p>Rephrasing all that, we don’t want users to call break without an enclosing boundary. That’s why break requires an in-scope given instance of Label, which the implementation of boundary.apply creates before it calls the code block you provide. If your code block calls break, a given Label will be in-scope.</p><p>You don’t have to do anything to create the context function passed to boundary.apply. It is synthesized from your block of code automatically when boundary.apply is called.</p><p>Look at firstIndex again. If we do find an element that is equal to elem, then we call break to return the index i from the boundary. If we don’t find the element, then a “normal” return is used to return -1. We never reach the -1 expression if break is called.</p><p>The boundary and break mechanism is a better alternative to scala.util.control.NonLocalReturns and scala.util.control.Breaks which are deprecated as of Scala 3.3.0. The new mechanism is easier for developers to use and it adds the following additional benefits:</p><ul><li>The implementation uses a new <a href="https://www.scala-lang.org/api/3.3.0/scala/util/boundary$Break.html">scala.util.boundary$.Break</a> class that derives from RuntimeException. Therefore, non-localbreaks are logically implemented as non-fatal exceptions and the implementation is optimized to suppress unnecessary stack trace generation. Stack traces are unnecessary because we are handling these exceptions, not barfing them on the user!</li><li>Better performance is provided when a break occurs to the enclosing scope inside the same method (i.e., the same stack frame), where it can be rewritten to a jump call.</li></ul><h3>Error Handling</h3><p>Next Martin discussed new ways of handling errors that leverage boundary and break, and are partly inspired by the way Rust handles errors.</p><p>He used a simpler example of trying a computation that will hopefully return a result wrapped in a Some, but if it can’t succeed, then it will return None. Hence, an “optional” result, if you will. (This implementation is also now in the book’s <a href="https://github.com/deanwampler/programming-scala-book-code-examples/blob/master/src/main/scala/progscala3/concurrency/direct/optional.scala">code examples</a>.)</p><pre>import scala.util.boundary, boundary.break, boundary.Label<br><br>object optional:<br>  inline def apply[T](inline body: Label[None.type] ?=&gt; T): Option[T] =<br>    boundary(Some(body))<br><br>  extension [T](r: Option[T])<br>    inline def ? (using label: Label[None.type]): T = r match<br>      case Some(x) =&gt; x<br>      case None =&gt; break(None)</pre><p>We lose all information about <em>why</em> it failed. A proper error handling feature should retain this information.</p><p>optional.apply defines a boundary by calling boundary.apply. The reason the Label is typed with None.type, is because if and when we call break, it is because we are returning None to the boundary. We don’t break if we successfully computed something to return in a Some , which will be returned normally.</p><p>Inspired by a similar-looking construct in Rust, if you have an Option instance, calling the new extension method ? on it will either return the enclosed object or call break to return None out of the enclosing boundary. So, while the ? method looks more generally useful for deconstructing an Option, it really requires us to decide what to do when it is a None. Here, we are inside a boundary and we use a break. Let’s look at an example to understand how it used.</p><p>Suppose you have a sequence (rows) of sequences (columns). You want to return a new sequence with just the first column, i.e., a sequence with the first element in each row. Because a row might be empty, we must wrap the returned sequence in an Option. If any row is empty, we’ll return None for the whole thing:</p><pre>def firstColumn[T](xss: Seq[Seq[T]]): Option[Seq[T]] =<br> optional:<br>   xss.map(_.headOption.?)</pre><p>The concision is very nice, but it requires some unpacking to understand what’s happening. optional defines the boundary, which will hopefully see a Some[Seq[T]] returned or, worst case, a None.</p><p>We map over the input sequence and for each nested sequence, we get the headOption, but then immediately extract the element using the new extension method ?. The map iteration will continue as long as those calls to ? return an instance of T, meaning the nested sequences are not empty. However, the first time an empty sequence is found, the implementation of ? will call break, immediately terminating the map iteration and returning a None out of the optional block.</p><p>Here’s what to expect:</p><pre> val xssSome = List(List(0), List(1,0), List(2,1,0), List(3,2,1,0))<br> val xssNone = List(List(0), Nil, List(2,1,0), List(3,2,1,0))<br> assert(firstColumn(xssSome) == Some(List(0,1,2,3)))<br> assert(firstColumn(xssNone) == None)</pre><p>To really appreciate this example, try writing the same method without the boundary and break mechanism. It’s certainly doable, but more tedious!</p><h3>Suspensions and a New Concurrency Library</h3><p>Back to the futures :) , boundary and break can be used for adding new concurrency abstractions to Scala following a direct style, like the Futures example above, as well <em>continuations, </em>which Martin is calling <em>suspensions</em>. I won’t all discuss the details he covered here, but I will mention a few topics.</p><p>The implementation of new concurrency features is not trivial for Scala, because:</p><ol><li>Scala now runs on three platforms: JVM, JavaScript, and native.</li><li>Even on the JVM, using the new lightweight fibers coming in <a href="https://wiki.openjdk.org/display/loom/Main">Project Loom</a> would only be available to users on the most recent JVMs (19 and later).</li></ol><p>Besides writing custom implementations for each of these scenarios, other possible implementations might use source or bytecode rewriting.</p><p>Work has started in the <a href="https://github.com/lampepfl/async">lampepfl/async</a> repo, a “strawman” for ideas, both for conceptual abstractions for concurrency (like a new Future type), as well as implementations. The plan is for this repo to evolve into a new concurrency library for Scala.</p><h3>Final Thoughts</h3><p>I’ve always liked Scala for the principled and deeply-considered approaches Martin and his team have taken to fundamental language and feature design. All programming language communities have ad hoc and “good enough” implementations for effects, like concurrency. Scala has very thoughtful systems for these purposes. However, we need to continue making it easier and more robust for people to write such code, which is commonplace and can’t be reserved for the few elite programmers.</p><p>For these reasons, Scala’s emerging <strong>direct style</strong> is a welcome trend.</p><blockquote><em>For a concise summary of the more “mainstream” notable changes in Scala 3, see my </em><a href="https://deanwampler.github.io/scala3-highlights.html"><em>Scala 3 Highlights</em></a><em> page.</em></blockquote><blockquote><em>See </em><a href="http://programming-scala.org/"><em>Programming Scala, Third Edition</em></a><em> for a comprehensive introduction to Scala 3, including details on how to migrate from Scala 2.</em></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d9c1bcb1f810" width="1" height="1" alt=""><hr><p><a href="https://medium.com/scala-3/scala-3-what-is-direct-style-d9c1bcb1f810">Scala 3: What Is “Direct Style”?</a> was originally published in <a href="https://medium.com/scala-3">Scala 3</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Scala 3: Even Fewer Braces]]></title>
            <link>https://medium.com/scala-3/scala-3-even-fewer-braces-3890e4f9bc55?source=rss----de7527e0301e---4</link>
            <guid isPermaLink="false">https://medium.com/p/3890e4f9bc55</guid>
            <category><![CDATA[scala-programming]]></category>
            <category><![CDATA[scala-3]]></category>
            <dc:creator><![CDATA[Dean Wampler]]></dc:creator>
            <pubDate>Sun, 20 Nov 2022 18:23:23 GMT</pubDate>
            <atom:updated>2022-11-20T18:23:23.782Z</atom:updated>
            <content:encoded><![CDATA[<p>One of the most significant, if controversial features in Scala 3 is <a href="https://medium.com/scala-3/scala-3-new-but-optional-syntax-855b48a4ca76">optional braces</a>, where significant indentation can be used instead of curly braces. However, there were a few syntax areas where braces were still required, such as passing anonymous functions and import statements. Scala 3.3 will remove the requirement that braces have to be used to pass anonymous functions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ro8gf3g8UxXI9k_IjaYOfg.jpeg" /><figcaption><a href="https://www.flickr.com/photos/deanwampler/52457598790/in/dateposted-public/">Fall Colors in Lincoln Park, Chicago</a>, © 2022, Dean Wampler</figcaption></figure><p>In essence, a colon, :, token is now also recognized as a marker where an anonymous function argument is defined next.</p><p>Let’s look at some examples, which I have added to the <a href="https://github.com/deanwampler/programming-scala-book-code-examples/blob/master/src/script/scala/progscala3/IndentationSyntax-FewerBraces.scala">Programming Scala code repo</a>. They are adapted from the Dotty documentation page:<br> <a href="https://dotty.epfl.ch/docs/reference/other-new-features/indentation.html">https://dotty.epfl.ch/docs/reference/other-new-features/indentation.html</a></p><p>The changes we will explore are already available as an <em>experimental</em> feature in Scala 3.2, which means that a special, experimental import statement is required.</p><p>However, the Scala compiler disallows use of experimental features in code when compiling with release builds. Instead, we need to use a <em>snapshot</em> release of the compiler. So, let’s see how to do that first. The steps are straightforward for our purposes:</p><ol><li>Clone the Dotty repo: <a href="https://github.com/lampepfl/dotty">https://github.com/lampepfl/dotty</a></li><li>Run the command: sbt dist/packArchive</li><li>Copy the dist/target/*.zip or dist/target/*.tar.gz file that is created somewhere convenient. For me, * was scala3–3.3.0-RC1-bin-SNAPSHOT.</li><li>Change to the new directory and expand the archive.</li><li>Change to the root directory created for the expanded archive. For me, this was scala3–3.3.0-RC1-bin-SNAPSHOT.</li><li>Run the command bin/scala</li></ol><p>Now we can copy and past the following examples.</p><p>First, we need to enable the feature:</p><pre>import language.experimental.fewerBraces</pre><p>This will not be needed starting with the 3.3 release. Actually, you might try omitting this import first, paste the “braceless” examples below, and see what errors are reported. I’ll just proceed with the “happy path”.</p><p>I’ll repeat each example twice, once with braces, as required for Scala 3.0 to 3.2, and again with the new syntax that eliminates the braces. This makes it easier to compare the differences.</p><p>First, here is an examplemethod that takes a function argument, which takes no arguments:</p><pre>def times(n: Int)(f: =&gt; Unit): Unit =<br>  for i &lt;- 0 until n do f<br><br>// Use braces to define and pass the function:<br>times(3) {<br>  println(&quot;one&quot;)<br>  println(&quot;two&quot;)<br>}<br>// (Output shown as comments)<br>// one<br>// two<br>// one<br>// two<br>// one<br>// two<br><br>// New braceless syntax to define and pass the function. Now the compiler<br>// interprets the trailing colon, followed by indented lines, as the <br>// beginning and definition of the anonymous function:<br>times(3):<br>  println(&quot;one&quot;)<br>  println(&quot;two&quot;)<br>// one<br>// two<br>// ...</pre><p>The output of each block of code is shown as comments. The first invocation uses braces. We see the required pattern in the second invocation; a colon followed by indented lines for the anonymous function body.</p><p>It works the same way for methods on types. Consider the ++ method on sequences:</p><pre>import java.io.File<br>val dir = new File(&quot;.&quot;)<br>// val dir: java.io.File = .<br><br>// Another example, where the `++` method expects a function argument:<br>val paths1 = Seq(dir) `++` {<br>  dir.listFiles<br>}<br>// val paths1: Seq[java.io.File] = List(., ./scala3-3.3.0-RC1-bin-SNAPSHOT, ./scala3-3.3.0-RC1-bin-SNAPSHOT.zip)<br><br>val paths2 = Seq(dir) `++`:<br>  dir.listFiles<br>// val paths2: Seq[java.io.File] = List(., ./scala3-3.3.0-RC1-bin-SNAPSHOT, ./scala3-3.3.0-RC1-bin-SNAPSHOT.zip)<br><br></pre><p>What about functions that take arguments? Let’s look at the familiar map and foldLeft methods:</p><pre>val xs = 0 until 10<br>// val xs: Range = Range 0 until 10<br><br>// The function arguments can either go on the next line after the colon<br>// or on the same line:<br>val map1a = xs.map {<br>  x =&gt;<br>    val y = x - 1<br>    y * y<br>}<br>// val map1a: IndexedSeq[Int] = Vector(1, 0, 1, 4, 9, 16, 25, 36, 49, 64)<br><br>val map2a = xs.map:<br>  x =&gt;<br>    val y = x - 1<br>    y * y<br>// val map2a: IndexedSeq[Int] = Vector(1, 0, 1, 4, 9, 16, 25, 36, 49, 64)</pre><p>It’s not especially nice having the argument list on a line by itself. Fortunately, this isn’t required. We can put the x =&gt; after the : on the same line:</p><pre>val map1b = xs.map { x =&gt;<br>  val y = x - 1<br>  y * y<br>}<br>// val map1b: IndexedSeq[Int] = Vector(1, 0, 1, 4, 9, 16, 25, 36, 49, 64)<br><br>val map2b = xs.map: x =&gt;<br>  val y = x - 1<br>  y * y<br>// val map2b: IndexedSeq[Int] = Vector(1, 0, 1, 4, 9, 16, 25, 36, 49, 64)<br><br>// It looks odd, but the arrow can be on the next line:<br>val map3b = xs.map: x<br>  =&gt;<br>    val y = x - 1<br>    y * y<br>// val map3b: IndexedSeq[Int] = Vector(1, 0, 1, 4, 9, 16, 25, 36, 49, 64)</pre><p>The last example shows we can still put the =&gt; on a new line if we want, but it looks weird.</p><p>Functions that take more than one argument work the same:</p><pre>val fold1a = xs.foldLeft(0) { (x, y) =&gt;<br>   x + y<br>}<br>// val fold1a: Int = 45<br><br>val fold2a = xs.foldLeft(0): (x, y) =&gt;<br>   x + y<br>// val fold2a: Int = 45</pre><p>Okay, so we can put the argument list on the same line as the :, but what about the single expression for the body?</p><pre>// scala&gt; val fold2b = xs.foldLeft(0): (x, y) =&gt; x + y<br>//      |<br>// -- Error: ---------------------------------------------------------------------------------------------------------------------------------------------------<br>// 1 |val fold2b = xs.foldLeft(0): (x, y) =&gt; x + y<br>//   |             ^^^^^^^^^^^^^^^^^^^^^^<br>//   |             not a legal formal parameter for a function literal</pre><p>That isn’t supported. If you want everything on one line like this, you’ll have to use braces:</p><pre>val fold1b = xs.foldLeft(0) {(x, y) =&gt; x + y}<br>// val fold1b: Int = 45</pre><p>So, if you really dislike colons, you’ll be able to avoid them when passing anonymous functions to methods.</p><p>What about import statements?</p><pre>scala&gt; import scala.util:    # : instead of .??<br>     |  Try, Success, Failure<br>     |<br>-- Error: ---------------------------------------------------------------------------------------------------------------------------------------------------<br>1 |import scala.util:<br>  |                 ^<br>  |                 end of statement expected but &#39;:&#39; found<br><br>scala&gt; import scala.util.:   # : after .??<br>     |  Try, Success, Failure<br>     |<br>-- [E040] Syntax Error: -------------------------------------------------------------------------------------------------------------------------------------<br>1 |import scala.util.:<br>  |                  ^<br>  |                  an identifier expected, but &#39;:&#39; found<br>  |<br>  | longer explanation available when compiling with `-explain`<br>-- [E040] Syntax Error: -------------------------------------------------------------------------------------------------------------------------------------<br>2 | Try, Success, Failure<br>  |             ^<br>  |             &#39;.&#39; expected, but &#39;,&#39; found<br>-- [E040] Syntax Error: -------------------------------------------------------------------------------------------------------------------------------------<br>3 |<br>  |^<br>  |&#39;.&#39; expected, but eof found</pre><p>Nope. You’ll still need braces:</p><pre>scala&gt; import scala.util.{Try, Success, Failure}<br>     |<br><br>scala&gt; import scala.{<br>     |  Option, Some, None<br>     | }<br>     |</pre><h4>Final Thoughts</h4><p>It’s nice to have the anonymous function “loophole” in the indentation syntax fixed. I’ll let you decide if this syntax is easier to read and understand versus the traditional syntax with braces.</p><blockquote>For a concise summary of the more “mainstream” notable changes in Scala 3, see my <a href="https://deanwampler.github.io/scala3-highlights.html">Scala 3 Highlights</a> page.</blockquote><blockquote>See <a href="http://programming-scala.org/">Programming Scala, Third Edition</a> for a comprehensive introduction to Scala 3, including details on how to migrate from Scala 2.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3890e4f9bc55" width="1" height="1" alt=""><hr><p><a href="https://medium.com/scala-3/scala-3-even-fewer-braces-3890e4f9bc55">Scala 3: Even Fewer Braces</a> was originally published in <a href="https://medium.com/scala-3">Scala 3</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Scala 3: “Erased” Definitions]]></title>
            <link>https://medium.com/scala-3/scala-3-erased-definitions-6130a30fbba4?source=rss----de7527e0301e---4</link>
            <guid isPermaLink="false">https://medium.com/p/6130a30fbba4</guid>
            <category><![CDATA[scala-programming]]></category>
            <category><![CDATA[scala-3]]></category>
            <category><![CDATA[scala]]></category>
            <dc:creator><![CDATA[Dean Wampler]]></dc:creator>
            <pubDate>Sat, 26 Mar 2022 16:52:58 GMT</pubDate>
            <atom:updated>2022-03-26T17:19:48.425Z</atom:updated>
            <content:encoded><![CDATA[<p>Martin Odersky and the EPFL Scala Center continue experimenting with potential new features in Scala 3. Let’s explore one of them, <em>erased definitions</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*94zQKHCwX4Pa-_MHtFvl4Q.jpeg" /><figcaption><a href="https://www.flickr.com/photos/deanwampler/51893887806/in/dateposted-public/">Red Clouds at Sunrise, Miami Beach</a> © 2022, Dean Wampler</figcaption></figure><p>This experimental feature is <a href="https://docs.scala-lang.org/scala3/reference/experimental/erased-defs.html#">described in the documentation</a>. Here, I’ll summarize its purpose, how it works, and provide an example of how it might be used.</p><p>A new keyword erased is introduced that can be applied to method arguments, including in using argument lists, method and value defintions, including givens, and even classes. The primary motivation for this feature is to provide a mechanism for constraining allowed “states” such that errors are caught at compile time, not runtime, but without extra overhead in the generated byte code. No byte code is generated for an “erased” definition. Hence, it can’t be used at runtime, e.g., referencing an erased value. It can only be used at compile time.</p><p>The <a href="https://docs.scala-lang.org/scala3/reference/experimental/erased-defs.html#">documentation</a> has a nice, simple example, where a type representing a machine can either be turned on or off, but the compiler will reject calling the method to turn off the machine if it is already off and similarly for the on state.</p><p>To see this in action, I decided to tackle a slightly more challenging problem. It’s always bugged me that Seq[T] has head, tail, and some other methods that should never be called on Nil. When you do that, an exception is thrown.</p><p>A good goal in API design is to <em>make it impossible for the user to make a mistake</em>. Ideally, you should only be able to call head or tail on a non-empty sequence and the compiler should enforce this. The library could define these methods only for non-empty sequences, but then you would have the usual problem that passing around a Seq[T] instance is practically useless, as it would not have many of the methods you need defined for it. Hence, defining methods like head and tail on Seq[T] is a pragmatic compromise. We avoid the potential error by using idioms like pattern matching or calling isEmpty to first determine if the sequence is empty or not, or use methods like headOption.</p><p>I explored a possible solution using dependent types in <a href="https://medium.com/scala-3/scala-3-dependent-types-part-ii-e7fc04dbfb08">Scala 3: Dependent Types, Part II</a>. (I guess I’m obsessive about this…) It worked, but the code was quite challenging to understand, at least for me, and I didn’t even attempt to define more than a few methods.</p><p>Instead, can we have the benefits of methods defined in Seq[T] that the <em>compiler will not allow us to call on </em>Nil. Oh, and no extra runtime overhead, please! Let’s see if erased meets these goals with a simpler implementation than my previous attempt.</p><p>The compiler won’t let you use experimental features at all unless you are using a nightly or snapshot build. So, I first cloned the <a href="https://github.com/lampepfl/dotty">dotty GitHub repo</a> locally, then executed sbt publishLocal to build it and put the artifacts in my local ivy2 repo. The version string was 3.1.3-RC1-bin-SNAPSHOT , which might be different for you when you try this.</p><p>Next, I created a new directory for the example. I used the following build.sbt, adapted from my <a href="https://github.com/deanwampler/programming-scala-book-code-examples">Programming Scala, Third Edition examples repo</a>. Change the definition of scala3 as required for your local build of Dotty:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/47dc780dab99c69a987de652d7b7300f/href">https://medium.com/media/47dc780dab99c69a987de652d7b7300f/href</a></iframe><p>I also created a file project/build.properties to specify the sbt version. It contains this line:</p><pre>sbt.version=1.6.2</pre><p>Next, I created a directory for the source files, src/main/scala/progscala3/erased, but you can certainly use any path you want.</p><p>Finally, here is the implementation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bf2a6813871d80b5571c470b754ab2a4/href">https://medium.com/media/bf2a6813871d80b5571c470b754ab2a4/href</a></iframe><p>Let’s work through this. The first import is an annotation that we’ll use to define the compiler error message. You can see it used for IsEmpty and IsNotEmpty. We also need to import the experimental feature.</p><p>The sealed trait Emptiness defines types that will be used as type parameters for IsEmpty and IsNotEmpty. Note the actual given instances for those two types: IsEmpty[Empty] and IsNotEmpty[NotEmpty] . Note they are declared with the new erased keyword. We <em>don’t </em>define given instances for the other two combinations: IsEmpty[NotEmpty] and IsNotEmpty[Empty], because they are <em>disallowed states</em>. These definitions follow the example in the <a href="https://docs.scala-lang.org/scala3/reference/experimental/erased-defs.html#">erased documentation</a>.</p><p>The defined given instances constrain what’s allowed by the compiler. This has been a common idiom, to limit what’s allowed to compile, implemented with implicits in Scala 2 and given/using constructs in Scala 3. Now we’re adding the ability to reduce the byte code footprint.</p><p>Note how ESeq is declared. I found it necessary to have two type parameters, one for the head element, H, and one for the tail sequence T. The latter is not needed for the library Seq, but I could not figure out how to avoid it, because it’s now necessary to carry some type information about whether or not the tail is empty, i.e., EmptySeq, as I call it here.</p><p>I found that using a type alias E works well, because each concrete subtype will either define E as NotEmpty or Empty.</p><p>The “magic” is in the methods head and tail. Each takes an <em>erased </em>using argument of type IsNotEmpty[E], which effectively means that calls to these methods <em>will be rejected at compile time unless</em> a suitable given instance is found in scope. For EmptySeq, no given instance will exist, specifically, IsNotEmpty[Empty] is <em>not </em>defined<em>. </em>We require a valid IsNotEmpty for the argument for both methods, but in the context of EmptySeq, the E type is Empty. The net effect is that calling head and tail on EmptySeq will <em>fail to compile</em>. I hope this makes sense…</p><p>Note that definitions for hd and tl are required in EmptySeq , but we can just use ???. They will <em>never be called</em>!</p><p>Carrying a type for the tail ESeq made the +: methods tricky. In essence, the type of the returned value is ESeq[new_head_type, whatever_this_is].</p><p>Finally, I found it convenient to only allow construction using the carefully defined NotEmptySeq.apply methods or the +: methods.</p><p>Let’s try it! Here is my ErasedSeqMain.scala file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b16a121c856bd1f7fb4594c3647b78ed/href">https://medium.com/media/b16a121c856bd1f7fb4594c3647b78ed/href</a></iframe><p>I won’t show the long output, but it is what you would expect. If you uncomment any of the lines with COMPILATION ERROR, you get <em>compilation errors</em>, not <em>runtime errors</em>, like the following for lines 64 and 65 (edited to reduce wrapping):</p><pre>[error] -- Error: .../src/main/scala/progscala3/erased/ErasedSeqMain.scala:64:67<br>[error] 64 |  println(s&quot;... = ${nes3b.tail.tail.tail.head}&quot;)  // COMPILATION ERROR<br>[error]    |                                             ^<br>[error]    |                         The seq must be not empty<br>[error] -- Error: .../src/main/scala/progscala3/erased/ErasedSeqMain.scala:65:67<br>[error] 65 |  println(s&quot;... = ${nes3b.tail.tail.tail.tail}&quot;)  // COMPILATION ERROR<br>[error]    |                                             ^<br>[error]    |                         The seq must be not empty</pre><p><em>How cool is that?!</em></p><p>Okay, I should point out that the byte code will carry more type information than for the library’s Seq[T], but I don’ t think that overhead can be eliminated. Try using the sbt console with some of those definitions and notice the types printed, for example (with no wrapping removed):</p><pre>scala&gt; import progscala3.erased.*<br>     |<br>     | val nes1 = &quot;one&quot; +: EmptySeq<br>     | val nes2 = &quot;two&quot; +: nes1<br>     | val nes3 = &quot;three&quot; +: nes2<br>     |<br>     | val nes1Head = nes1.head<br>     | val nes1Tail = nes1.tail<br>     |<br>     | val nes2Head = nes2.head<br>     | val nes2Tail = nes2.tail<br>     | val nes2TailHead = nes2.tail.head<br>     | val nes2TailTail = nes2.tail.tail<br>     |<br>     | val nes3Head = nes3.head<br>     | val nes3Tail = nes3.tail<br>     | val nes3TailHead = nes3.tail.head<br>     | val nes3TailTail = nes3.tail.tail<br>     |<br>val nes1: progscala3.erased.NotEmptySeq[String, progscala3.erased.EmptySeq.type] = one +: EmptySeq<br>val nes2: progscala3.erased.NotEmptySeq[String, nes1.type] = two +: one +: EmptySeq<br>val nes3: progscala3.erased.NotEmptySeq[String, nes2.type] = three +: two +: one +: EmptySeq<br>val nes1Head: String = one<br>val nes1Tail: progscala3.erased.EmptySeq.type = EmptySeq<br>val nes2Head: String = two<br>val nes2Tail:<br>  progscala3.erased.NotEmptySeq[String, progscala3.erased.EmptySeq.type] = one +: EmptySeq<br>val nes2TailHead: String = one<br>val nes2TailTail: progscala3.erased.EmptySeq.type = EmptySeq<br>val nes3Head: String = three<br>val nes3Tail: progscala3.erased.NotEmptySeq[String, nes1.type] = two +: one +: EmptySeq<br>val nes3TailHead: String = two<br>val nes3TailTail:<br>  progscala3.erased.NotEmptySeq[String, progscala3.erased.EmptySeq.type] = one +: EmptySeq</pre><p>It took me quite a bit of experimentation to make this work. As we saw in <a href="https://medium.com/scala-3/scala-3-dependent-types-part-ii-e7fc04dbfb08">Scala 3: Dependent Types, Part II</a>, some of the newer features in Scala are quite powerful, but nontrivial to use. However, this erased sequence ESeq is easier to understand than the DTList implementation in the previous post, which is nice. It remains to be seen how much adoption these advanced features actually experience, going forward.</p><blockquote>For a concise summary of the more “mainstream” notable changes in Scala 3, see my new <a href="https://deanwampler.github.io/scala3-highlights.html">Scala 3 Highlights</a> page.</blockquote><blockquote>See <a href="http://programming-scala.org/">Programming Scala, Third Edition</a> for comprehensive introduction to Scala 3, including details on how to migrate from Scala 2.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6130a30fbba4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/scala-3/scala-3-erased-definitions-6130a30fbba4">Scala 3: “Erased” Definitions</a> was originally published in <a href="https://medium.com/scala-3">Scala 3</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Safer Exceptions in Scala 3]]></title>
            <link>https://medium.com/scala-3/safer-exceptions-in-scala-3-12c47100f050?source=rss----de7527e0301e---4</link>
            <guid isPermaLink="false">https://medium.com/p/12c47100f050</guid>
            <category><![CDATA[scala-programming]]></category>
            <category><![CDATA[scala]]></category>
            <category><![CDATA[scala-3]]></category>
            <dc:creator><![CDATA[Dean Wampler]]></dc:creator>
            <pubDate>Sun, 16 Jan 2022 21:14:28 GMT</pubDate>
            <atom:updated>2022-01-21T13:25:44.599Z</atom:updated>
            <content:encoded><![CDATA[<p>I mentioned in <a href="https://medium.com/scala-3/scala-3-whats-changed-since-scala-3-0-0-be0830c059f5">What’s Changed Since Scala 3.0.0</a> that Scala 3.1 introduced an experimental feature for <em>safer exceptions</em>. Let’s explore it in more detail.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*motfTMl6MXI2_5sPVodouw.jpeg" /><figcaption><a href="https://www.flickr.com/photos/deanwampler/51774898376/">Ornament</a>, © 2021, Dean Wampler</figcaption></figure><p>Java’s <a href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-11.html">checked exceptions</a> were an attempt to express in method signatures the possibility that an exception might be thrown. While laudable in theory, in practice the requirement to add throws clauses everywhere or handle the exception “quickly” became burdensome, leading most developers to avoid using checked exceptions. <em>Unchecked</em> exceptions, which are RuntimeException and its subclasses and Error and its subclasses, became the norm in most Java code.</p><p>I think part of the problem was unprincipled use of exceptions in software designs, but I’ll come back to that below.</p><p>Using unchecked exceptions, which is basically how Scala has treated all exceptions, eliminated the tedium, but did nothing to make exceptions part of the type signature for methods. This is a big practical hole, as far as program correctness is concerned.</p><p>Instead, the Scala community has encouraged handling “exceptional” cases <em>monadically. </em>For example, Map.get(key) returns an Option[V], instead of a V (for a value of type V). None is returned if a value doesn’t exist for the key, instead of throwing an exception.</p><p>More generally, Either[E,V] has been used as a return type with the convention that returning Left[E] holds an error E, such as an exception, while Right[V] returns a successful value of type V. Try[V] works similarly.</p><p>Among other benefits, the monadic approach treats errors as first class values using the same method return (i.e., stack popping) infrastructure used for normal return values. Discontinuous stack jumps used for exception throwing are avoided. The caller must examine the Option, Either, or Try to determine success or failure and take the correct action.</p><p>Still, you can argue there are times when exceptions are useful for jumping out of a low-level method to some higher-level method that knows what to do if an exception occurs.</p><p>Scala 3.1 introduced an experimental feature to add declaring of checked exceptions back into Scala, but use a different mechanism to ensure they are handled.</p><p>Let’s walk through an example. First consider the following code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a84f685a6a1817f800ad4ddc49ef34e5/href">https://medium.com/media/a84f685a6a1817f800ad4ddc49ef34e5/href</a></iframe><p>This simple program takes a list of file names and prints their sizes in bytes, or throws an exception for each one that doesn’t exist.</p><p>We need the @experimental annotation on any type or individual method that uses an experimental feature, like <em>safer exceptions</em>. We enable the language feature with the import on line 5.</p><p>Note the declaration of openExistingFile, which has the comment // &lt;1&gt;. We can add a throws clause for this method that will throw a Java IOException if the specified file doesn’t exist. <em>The return type explicitly indicates an </em><em>IOException might be thrown.</em></p><p>The feature is actually implemented as an <em>implicit capability</em>, along with some <em>syntactic sugar</em> provided by the compiler<em>.</em></p><p>The term <em>capability</em> is used in the following sense, <em>the method is made capable of throwing an IOException.</em> It is given this capability through an implicit (or using) parameter. In fact, the following two declarations are equivalent:</p><pre>def openExistingFile(fileName: String): File throws IOException<br>def openExistingFile(fileName: String)(using CanThrow[IOException]): File</pre><p><a href="https://scala-lang.org/api/3.x/scala/CanThrow.html">CanThrow</a> is a new class in the library. If a given instance of it for IOException is in scope for calls to openExistingFile, then the <em>capability</em> is provided for the method to throw this exception. The compiler provides the <em>syntactic sugar</em> allowing us to write File throws IOException instead of a regular given argument list.</p><blockquote>I picked the checked exception IOException for this example, because this feature only works with JVM checked exceptions. It is not intended to be used with unchecked exceptions.</blockquote><p>As currently written, there is no given instance in scope, so compiling this code produces the following error:</p><pre>[error] -- Error: .../SaferExceptions.scala:13:41<br>[error] 13 |    val file = openExistingFile(fileName)<br>[error]    |                                         ^<br>[error]    |The capability to throw exception java.io.IOException is missing.<br>[error]    |The capability can be provided by one of the following:<br>[error]    | - A using clause `(using CanThrow[java.io.IOException])`<br>[error]    | - A `throws` clause in a result type such as `X throws java.io.IOException`<br>[error]    | - an enclosing `try` that catches java.io.IOException<br>[error]    |<br>[error]    |The following import might fix the problem:<br>[error]    |<br>[error]    |  import unsafeExceptions.canThrowAny<br>[error]    |<br>[error] one error found</pre><p>When introducing this feature incrementally, the easiest fix is the last one suggested, import unsafeExceptions.canThrowAny. This import provides a given that works for all checked exceptions. It is intended as a temporary fix, until you add the handlers, such as the suggested try/catch wrapper.</p><p>If you add the import and run it with arguments for a file that exists (say build.sbt) and one that doesn’t (badfile), you get this output (using sbt with Scala 3.1.0; see also the final callout below):</p><pre>sbt&gt; runMain ...SaferExceptions build.sbt badfile<br>[info] running (fork) ...SaferExceptions build.sbt badfile<br>[info] file build.sbt (.../build.sbt) has 4434 bytes.<br>[error] Exception in thread &quot;main&quot; java.io.IOException: badfile doesn&#39;t exist!<br>[error]   at progscala3.rounding.saferexceptions.SaferExceptions$package$.openExistingFile(SaferExceptions.scala:30)<br>[error]   at progscala3.rounding.saferexceptions.SaferExceptions$.main$$anonfun$1(SaferExceptions.scala:21)<br>[error]   at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)<br>[error]   at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)<br>[error]   at scala.collection.ArrayOps$.foreach$extension(ArrayOps.scala:1323)<br>[error]   at ...SaferExceptions$.main(SaferExceptions.scala:24)<br>[error]   at ...SaferExceptions.main(SaferExceptions.scala)<br>[error] Nonzero exit code returned from runner: 1<br>[error] (Compile / runMain) Nonzero exit code returned from runner: 1</pre><p>We see that build.sbt is 4434 bytes long and we get a familiar stack trace for the unhandled exception thrown for badfile.</p><p>Doing <em>the right thing™, </em>adding atry/catch clause, looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d99502a7f4af6d1ae6c098bb68252c70/href">https://medium.com/media/d99502a7f4af6d1ae6c098bb68252c70/href</a></iframe><p>The compiler generates a given CanThrow[IOException] for us, because of the catch clause with a case ioe: IOException. If you run this version, you get the following, more user-friendly output:</p><pre>sbt&gt; runMain ...SaferExceptions build.sbt badfile<br>[info] compiling 1 Scala source to .../target/scala-3.1.1-RC2/classes ...<br>[info] running (fork) ...SaferExceptions build.sbt badfile<br>[info] file build.sbt (.../build.sbt) has 4434 bytes.<br>[info] file badfile: IOException caught: badfile doesn&#39;t exist!</pre><p>Incidentally, if you remove the throws IOException clause from the declaration of openExistingFile, you get the following compilation error:</p><pre>[error] -- Error: .../SaferExceptions.scala:43:35<br>[error] 43 |    if file.exists() == false then throw new IOException(s&quot;$fileName doesn&#39;t exist!&quot;)<br>[error]    |                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br>[error]    |The capability to throw exception java.io.IOException is missing.<br>[error]    |The capability can be provided by one of the following:<br>[error]    | - A using clause `(using CanThrow[java.io.IOException])`<br>[error]    | - A `throws` clause in a result type such as `X throws java.io.IOException`<br>[error]    | - an enclosing `try` that catches java.io.IOException<br>[error]    |<br>[error]    |The following import might fix the problem:<br>[error]    |<br>[error]    |  import unsafeExceptions.canThrowAny<br>[error]    |<br>[error] one error found</pre><p>Looks familar. Now that we have enabled saferExceptions, the compiler won’t allow the method to throw a checked IOException unless we use the <em>escape hatch</em> import unsafeExceptions.canThrowAny or we add either the throws IOException or ausing CanThrow[IOException] argument list to the method signature.</p><p>Finally, what about nested calls?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d8a7dbeadad1942c36f8a7851579d5f6/href">https://medium.com/media/d8a7dbeadad1942c36f8a7851579d5f6/href</a></iframe><p>If you try to declare wrapper without the throws IOException, it won’t compile. Hence, <em>safer exceptions</em> does not change the Java-style requirement that each method in the call chain must either declare it throws the exception or it must handle checked exceptions thrown by methods it calls.</p><p>What this feature does provide is the ability to declare checked exception can be thrown and it leverages the implicit mechanism to enable this <em>capability, </em>a mechanism that might be extended to other <a href="https://docs.scala-lang.org/scala3/reference/experimental/canthrow.html"><em>effects</em></a><em>.</em></p><h4>Are We Using Exceptions the Way that We Should?</h4><p>So, we still don’t have a way to say something like, “this low level method I’m writing might throw a checked exception, but I want all methods above me in the stack to be oblivious to this possibility, except for some high-level method capable of handling the error, which I promise to provide…”</p><p>There are really two things wrong with this “quote”. First, it is impossible for the Scala compiler to enforce my promise, because the compiler doesn’t have the global program scope available. How could it, when we build libraries and other incremental deliveries?</p><p>I think the real problem, though, is the first part of this quote, where I give myself permission to do risky operations at a low level that might throw such an exception.</p><blockquote>Could it be that the “pain” of checked exceptions is really a signal to us that our design is sloppy?</blockquote><p>I’ve seen many large code bases where low-level code makes calls to external APIs (file systems, databases, cluster services, etc.), validates that some string can be parsed into an integer, etc., etc. There is little discipline about where such logic belongs and where it doesn’t. <em>I claim this is the real problem that needs solving!</em></p><p>We need to discipline ourselves to write modules that only perform “risky” operations near their boundaries, at the beginning of the work they are asked to do, not deep in the call stack. Once you go below a few levels, the data required should be complete, transformed, validated, etc., so it’s nearly impossible to throw an exception (excepting OOMs and catastrophes of that sort).</p><p>This discipline would also force us to write “flatter” code by removing unnecessary layers.</p><h4>A Few More Details and Where to Go for More Information</h4><p>If a method can throw more than one checked exception, use a <em>union type</em>:</p><pre>def m(stuff: Seq[Thing]): OtherThing throws E1 | E2 | E3</pre><p>More details are described in this <a href="https://docs.scala-lang.org/scala3/reference/experimental/canthrow.html">scala-lang.org page</a>. Note that it currently contains an error; it uses an earlier syntax OtherThing canThrow E, where now throws is used.</p><p>The research work behind this feature is described in <a href="https://infoscience.epfl.ch/record/290885?&amp;ln=en">this paper</a> (<a href="https://dl.acm.org/doi/10.1145/3486610.3486893">ACM citation</a>). The first half is worth reading yourself for details I didn’t cover. The second half dives into potential improvements to Scala’s type system to address limitations of the current solution using the existing type sytem. It is more advanced reading…</p><blockquote><em>This feature is too new to be discussed in </em><a href="http://programming-scala.com/"><em>Programming Scala, Third Edition</em></a><em>, but I have added an example to the book’s code repo </em><a href="https://github.com/deanwampler/programming-scala-book-code-examples/tree/master/src/main/scala/progscala3/rounding/saferexceptions"><em>here</em></a><em>.</em></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=12c47100f050" width="1" height="1" alt=""><hr><p><a href="https://medium.com/scala-3/safer-exceptions-in-scala-3-12c47100f050">Safer Exceptions in Scala 3</a> was originally published in <a href="https://medium.com/scala-3">Scala 3</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Value of Parametric Polymorphism]]></title>
            <link>https://medium.com/scala-3/the-value-of-parametric-polymophism-e76bfb9a516b?source=rss----de7527e0301e---4</link>
            <guid isPermaLink="false">https://medium.com/p/e76bfb9a516b</guid>
            <category><![CDATA[scala-programming]]></category>
            <category><![CDATA[scala]]></category>
            <category><![CDATA[scala-3]]></category>
            <dc:creator><![CDATA[Dean Wampler]]></dc:creator>
            <pubDate>Sun, 10 Oct 2021 18:49:26 GMT</pubDate>
            <atom:updated>2021-10-10T18:52:36.437Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8QyAOxovJj7n8CMLen2cMQ.jpeg" /><figcaption><a href="https://www.flickr.com/photos/deanwampler/51395802128/">Blue Angels in Chicago</a>, © 2021, Dean Wampler</figcaption></figure><blockquote><em>I’m giving a talk at </em><a href="https://www.scale.bythebay.io/"><em>Scale by the Bay, October 28th</em></a><em>, on </em>Lessons Learned from 15 Years of Scala in the Wild.</blockquote><blockquote><a href="http://programming-scala.com/"><em>Programming Scala, Third Edition</em></a><em> is now available. It provides a comprehensive introduction to Scala 3 for experienced Scala developers, as well as a complete introduction to Scala for new Scala developers.</em></blockquote><p>The other day, I tweeted the following:</p><h3>Dean Wampler on Twitter: &quot;Pro tip: If you work with Java, Scala, or other statically typed language that supports type parameters (i.e., &quot;parametric polymorphism&quot;), take the time to learn how to write parameterized types and methods. The amount of boilerplate reduction can be gigantic. / Twitter&quot;</h3><p>Pro tip: If you work with Java, Scala, or other statically typed language that supports type parameters (i.e., &quot;parametric polymorphism&quot;), take the time to learn how to write parameterized types and methods. The amount of boilerplate reduction can be gigantic.</p><p>A friend replied that I should provide an example, so here you go… I’ll use Scala 3 syntax, but the arguments apply equally well to Scala 2, to Java, and most other statically-typed languages. <a href="https://github.com/deanwampler/parametric-polymorphism">This repo</a> has the Scala 3 and Java examples. For brevity, I’ll just discuss the Scala 3 example.</p><p>While I’ll focus on the boilerplate reduction aspect, this post ends with thoughts about the real “superpower” of parametric polymorphism, which might be less familiar to many of you.</p><p>First, what is <em>parametric polymorphism</em>? You probably already understand <em>subtype polymorphism, </em>the object-oriented notion of polymorphism, where an abstraction is defined, say with Java interface or Scala trait, then multiple concrete implementations provide the specific details that vary for each of those types. This is one of several kinds of polymorphism that I discuss in <a href="http://programming-scala.com/"><em>Programming Scala, Third Edition</em></a><em>. Parametric polymorphism</em> (also discussed at length in the book) is today’s subject, where a type parameter is the abstraction and implementations are created (by you or by the compiler deities) for specific implementations.</p><p>For example, suppose you didn’t trust the size method for Scala collections 🤓. Here is a trivial example of a parametrically-polymorphic method alternative:</p><pre>$ scala<br>Starting scala3 REPL...<br>scala&gt; def len[T](xs: Seq[T]): Int = xs.foldLeft(0)((count, _) =&gt; count + 1)<br>     |<br>def len[T](xs: Seq[T]): Int</pre><pre>scala&gt; len(Seq(1,2,3,4,5))<br>val res0: Int = 5</pre><p>We obviously don’t care about the type of elements. You could even rewrite the method signature as def len2(xs: Seq[?]): Int and the same implementation would still work. I’ll show a more interesting example at the end, but the point here is that I specify a type parameter T and the method works for any Seq[T] I provide. I don’t need separate methods for Seq[Int], Seq[Double], etc.</p><p>Okay, now suppose a define some messages that can be sent between subsystems, where the receiver of a message needs to handle it properly. First, here are the “incoming” and “outgoing” message types:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dc462c333b1cd23a06c15ba2d61b57be/href">https://medium.com/media/dc462c333b1cd23a06c15ba2d61b57be/href</a></iframe><p>Why didn’t I use the new fancy enum feature in Scala 3? For details I won’t go into here, there’s a slight disadvantage in the way typing works for enums vs. the “classic” approach shown here. (Also, we wouldn’t be able to support adding new IncomingMessage types with enums). See the <a href="https://github.com/deanwampler/parametric-polymorphism/tree/with-enums">repo branch </a><a href="https://github.com/deanwampler/parametric-polymorphism/tree/with-enums">with-enums</a> and the code comments there for the details of the typing difference and the additional code required if you use enums.</p><p>Okay, here is the first version (v1) of how we might work with these messages. First, I need message handlers:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e6d7fd202b1399bcf78aa618bfed7ac2/href">https://medium.com/media/e6d7fd202b1399bcf78aa618bfed7ac2/href</a></iframe><p>I start with a simple trait that defines the handler abstraction, then I define instances for each kind of IncomingMessage. Yes, the apply method bodies are all identical and I could have just provided a concrete MessageHandler.apply body. Realistically, the bodies won’t be identical. I’ll come back to this below.</p><p>Now let’s use the implementation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8c57460f8c5d38bfae306803cc77ad7c/href">https://medium.com/media/8c57460f8c5d38bfae306803cc77ad7c/href</a></iframe><p>The Processor.apply has to pattern match on the message and call the write one. It’s a bit tedious, but it is straightforward to implement and to read when you come back to the code later. That’s a virtue we shouldn’t ignore, even though we’ll eliminate the need for this pattern matching in a moment.</p><p>If you start sbt and run it, entering 1 at the prompt, here’s what you get:</p><pre>sbt:parametric-polymorphism&gt; run</pre><pre>Multiple main classes detected. Select one to run:<br> [1] polyglotprogramming.messaging.v1.Messaging<br> [2] polyglotprogramming.messaging.v2.Messaging<br> [3] polyglotprogramming.messaging.v3.Messaging<br> [4] polyglotprogramming.messaging.v4.Messaging<br> [5] polyglotprogramming.messaging.v4java.Main</pre><pre>Enter number: 1<br>[info] running polyglotprogramming.messaging.v1.Messaging<br>Received message: Start(start)<br>Succeeded(Successfully processed Start(start))<br>Received message: DoWork1(dowork1)<br>Succeeded(Successfully processed DoWork1(dowork1))<br>Received message: DoWork2(dowork2)<br>Succeeded(Successfully processed DoWork2(dowork2))<br>Received message: Stop(stop)<br>Succeeded(Successfully processed Stop(stop))</pre><p>Now let’s use parametric polymorphism to reduce the size of this code, then discover other benefits. This is v2. The messages are the same. For the moment, I’ll go ahead and use a single implementation of MessageHandler.apply, then return to the general case where that won’t work (in v3):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/47d8641898f37d02498254d6d529a038/href">https://medium.com/media/47d8641898f37d02498254d6d529a038/href</a></iframe><p>It’s drastically smaller. We still need concrete instances for each IncomingMessage type, but now we can use concise given instances.</p><p>Here is the updated Processor and an identical @main method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e28456aeab10df66160182e725b42b69/href">https://medium.com/media/e28456aeab10df66160182e725b42b69/href</a></iframe><p>Processor.apply works for any type IM that is a subtype of IncomingMessage. The message argument is typed specifically to be IM, too. The “secret sauce” is the addition of a using parameter list where a corresponding MessageHandler[IM] must be in scope. We imported the given instances we need using the second import statement on line 3. Not only have we eliminated some boilerplate (it would be even more noticable in a less trivial example…), but we have also eliminated the need to match in IncomingMessages.</p><p>How can we handle different MessageHandler.apply implementations? The <a href="http://://en.wikipedia.org/wiki/Template_method_pattern"><em>Template Method Pattern</em></a> is our friend. Here’s our v3 iteration:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f6c9aa981d1b9df6c1d40d30e1f7a400/href">https://medium.com/media/f6c9aa981d1b9df6c1d40d30e1f7a400/href</a></iframe><p>MessageHandler.apply is still concrete and I’ve now declared it to be final, to emphasize that users shouldn’t override a concrete method, because it’s very easy to break its “contract”. Instead, the protected process method is declared for variations in handling. There are also two protected helper methods, success and failure.</p><p>Each given instance only needs to define what’s unique about the way it handles messages, by implementing process. Now, I’ll assume that all handling of DoWork2 messages fail. For all four implementations of process, a unique “details” string is returned.</p><p>I won’t show the corresponding <a href="https://github.com/deanwampler/parametric-polymorphism/blob/main/src/main/scala/polyglotprogramming/messaging/v3/Main.scala">v3/Main.scala</a>, as it identical to the v2 implementation, except for the package name and import statement.</p><p>This code uses parametric polymorphism to define uniform handling for all allowed subtypes of IncomingMessage, while providing the hooks required for implementing variant behavior.</p><p>Finally, let’s see how a new IncomingMessage type can be supported (v4). <a href="https://github.com/deanwampler/parametric-polymorphism/blob/main/src/main/scala/polyglotprogramming/messaging/v4/MessageHandlers.scala">v4/MessageHandlers.scala</a>is identical to the v3 implementation, except for the package name. Here’s the new Main.scala:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9d9786f3aabcece3009f13683f36ea39/href">https://medium.com/media/9d9786f3aabcece3009f13683f36ea39/href</a></iframe><p>Processor is unchanged. It continues to work as long as the input message and handler have the correct types. A new message type DoWork3 is declared along with a corresponding given instance for it. Messaging() adds an instance of DoWork3 and everything works as before! Note the comment; if you forget to define a matching given instance, you’ll get a compile time (vs. runtime) error.</p><h4>Recap</h4><p>I eliminated all the original boilerplate code using parametric polymorphism, while preserving the flexibility necessary for variant behavior, using the <em>Template Method Pattern </em>(perhaps my favorite <em>Gang of Four</em> pattern)<em>. </em>Yes, the simple example wasn’t that verbose to begin with, but hopefully you can see how this could make a real difference in many real-world problems.</p><p>I also eliminated the pattern matching that was required in v1, along with the need to modify that code if and when a new message type is added! In v4, I exploited the “pluggable” nature of Producer. All I needed besides the new IncomingMessage type was a given instance of a corresponding MessageHandler. So, it is easy to add new message types, but it’s also robust against an easy mistake to make. A compile-time error is thrown if I forget to add a given instance of a MessageHandler!</p><p>It’s not quite as easy to do parametric polymorphism in Java and you don’t have given instances and using clauses to keep the code in Messaging() as clean and concise, but you can still leverage these ideas. See the <a href="https://github.com/deanwampler/parametric-polymorphism/tree/main/src/main/java/polyglotprogramming/messaging/v4java">Java implementation</a> I provided in the repo. (My Java is rusty; PRs for improvements are welcome!)</p><h3>The Real “Superpower” of Parametric Polymorphism</h3><p>Okay, this post was inspired by the need to keep code as concise and still robust as possible and how parametric polymorphism gives you powerful tools for this purpose. However, there is really an even more important “superpower” that parametric polymorphism gives us.</p><p>Remember the “trivial” len method I showed at the beginning? Here’s the signature again, this time with a generic name and omitting the body:</p><pre>def foo1[T](xs: Seq[T]): Int</pre><p>Hmm. What if I didn’t have parametric polymorphism and I wrote something like this instead:</p><pre>def foo2(xs: SeqOfInt): Int  // SeqOfInt is a sequence of integers</pre><p>Forget the fact we started with len and ask yourself, what are the possible, “legal” implementations for each of these methods?</p><p>foo2 could be <em>len</em>, but also <em>sum</em>, <em>multiple</em>, <em>median</em>, and others, whereas <em>there is really only one possible implementation for </em>foo1, the length of the collection.</p><blockquote>By being more abstract in the types of the elements, we constrain the allowed implementations.</blockquote><p>The real power of parametric polymorphism is the way it makes our type and method signatures much more precise, <em>even while they become more flexible for use with different types</em>. It becomes much harder to write giant hairball methods that mix all kinds of functionality, creating a morass that’s hard to understand, test, debug, and reuse. It brings greater discipline and precision to our designs.</p><p>All that said, len (a.k.a foo1) is kind of trivial. Here’s a more sophisticated case that still leverages the same power. What if the collection elements are “numeric”, i.e., <a href="https://scala-lang.org/api/3.x/scala/math/Numeric.html">Numeric[T]</a> exists for the element type T? Here’s how we can multiple any such sequence of elements:</p><pre>scala&gt; def multiply[T : Numeric](xs: Seq[T]): T =<br>     |   xs.reduceLeft((x1,x2) =&gt; summon[Numeric[T]].times(x1,x2))<br>     |<br>def multiply[T](xs: Seq[T])(using evidence$1: Numeric[T]): T</pre><pre>scala&gt; multiply(Seq(1,2,3,4,5))<br>val res0: Int = 120</pre><pre>scala&gt; multiply(Seq(1.1,2.2,3.3,4.4,5.5))<br>val res1: Double = 193.26120000000003</pre><p>Now the type parameter is constrained by a <em>context bound</em>. (I discussed <em>context bounds </em>and what summon does <a href="https://medium.com/scala-3/scala-3-contextual-abstractions-part-ii-35174c7f1baa">here</a>.) There must exist a Numeric[T] for any concrete type T we use. These are predefined in the Scala library for Int and Double . You can define your own Numeric[T] for your own types, like Matrix, Rational, GeoCoordinates, etc.</p><p>Change the method name to foo again and ask yourself, what are the possible implementations allowed by the method signature? In this case, it’s not just one implementation, but the defined operations that Numeric[T] provides where the corresponding methods return T: addition, subtraction, multiplication, etc.</p><p>This is just the tip of the Iceberg. You can find plenty of references on the web that expound upon this idea. See for example, the great talk by Rúnar Bjornasson called <a href="https://www.youtube.com/watch?v=GqmsQeSzMdw"><em>Constraints Liberate, Liberties Contrain</em></a><em> </em>and the book he cowrote with Paul Chiusano called <a href="https://www.manning.com/books/functional-programming-in-scala"><em>Functional Programming in Scala</em></a><em>.</em> (A <a href="https://www.manning.com/books/functional-programming-in-scala-second-edition">second edition</a> is under development.)</p><blockquote><em>For more on parametric and subtype polymorphisms in Scala, see </em><a href="http://programming-scala.com/"><em>Programming Scala, Third Edition</em></a><em> and join me at </em><a href="https://www.scale.bythebay.io/"><em>Scale by the Bay, October 28th</em></a><em>!</em></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e76bfb9a516b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/scala-3/the-value-of-parametric-polymophism-e76bfb9a516b">The Value of Parametric Polymorphism</a> was originally published in <a href="https://medium.com/scala-3">Scala 3</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Scala 3: What’s Changed Since Scala 3.0.0]]></title>
            <link>https://medium.com/scala-3/scala-3-whats-changed-since-scala-3-0-0-be0830c059f5?source=rss----de7527e0301e---4</link>
            <guid isPermaLink="false">https://medium.com/p/be0830c059f5</guid>
            <category><![CDATA[scala]]></category>
            <category><![CDATA[scala-programming]]></category>
            <category><![CDATA[scala-3]]></category>
            <dc:creator><![CDATA[Dean Wampler]]></dc:creator>
            <pubDate>Sat, 04 Sep 2021 20:38:26 GMT</pubDate>
            <atom:updated>2021-09-04T20:38:25.929Z</atom:updated>
            <content:encoded><![CDATA[<p>The Scala team continues to refine Scala 3, fixing bugs, refining existing features, and introducing some experimental features for Scala 3.1. Here are a few highlights.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F-Uj6kMDDQq1MdCIVMlQ1A.jpeg" /><figcaption><a href="https://www.flickr.com/photos/deanwampler/51395802018/in/dateposted-public/">Green light</a>, © 2021, Dean Wampler</figcaption></figure><blockquote><a href="http://programming-scala.com/"><em>Programming Scala, Third Edition</em></a><em> is now available. It provides a comprehensive introduction to Scala 3 for experienced Scala developers, as well as a complete introduction to Scala for new Scala developers.</em></blockquote><h3>Scala 3.0.1</h3><p>This <a href="https://github.com/lampepfl/dotty/releases/tag/3.0.1">patch release</a> mostly fixes bugs and makes small refinements that don’t impact the book’s contents, with two exceptions I’ll discuss here.</p><h4>Simplified given Syntax</h4><p>In Scala 3.0.0, if you want to declare a given for a class with no refinement required (i.e., defining an abstract method), you have to use one of two, slightly awkward syntax options. For example, in the <a href="https://github.com/deanwampler/programming-scala-book-code-examples">book’s code examples</a>, the <a href="https://github.com/deanwampler/programming-scala-book-code-examples/blob/master/src/main/scala/progscala3/contexts/json/JSONBuilder.scala">JSONBuilder.scala</a> example defines a trait ValidJSONValue to enumerate allowed types for building JSON from data structures. Here is how the trait and giveninstances are defined in the book, using Scala 3.0.0 syntax:</p><pre>sealed trait ValidJSONValue[T &lt;: Matchable]<br>given ValidJSONValue[Int] with {}<br>given ValidJSONValue[Double] with {}<br>given ValidJSONValue[String] with {}<br>given ValidJSONValue[Boolean] with {}<br>given ValidJSONValue[JSONObject] with {}<br>given ValidJSONValue[JSONArray] with {}</pre><p>Having to write with {} is inconvenient and non-obvious, too. You can also define given instances using given ValidJSONValue[Int] = ValidJSONValue[Int](), for example, but it seems a bit counterintuitive that you have to write the type twice.</p><p>Scala 3.0.1 <a href="https://github.com/lampepfl/dotty/pull/12107">introduces</a> this new, more concise alternative:</p><pre>sealed trait ValidJSONValue[T &lt;: Matchable]<br>given ValidJSONValue[Int]()<br>given ValidJSONValue[Double]()<br>given ValidJSONValue[String]()<br>given ValidJSONValue[Boolean]()<br>given ValidJSONValue[JSONObject]()<br>given ValidJSONValue[JSONArray]()</pre><p>(The latest commits in the examples repo use this updated syntax.)</p><h4>@experimental annotation</h4><p>Scala 3.0.1 <a href="https://github.com/lampepfl/dotty/pull/12102">introduces</a> a new @experimental annotation that is used to mark definitions for experimental features. They can be used in the same situations where language.experimental can be used. As stated in the <a href="https://github.com/lampepfl/dotty/pull/12102">pull request</a>,</p><p>A class is experimental if</p><ul><li>It is annotated with @experimental</li><li>It is a nested class of an experimental class. Annotation @experimental is inferred.</li><li>It extends an experimental class. An error is emitted if it does not have the annotation.</li></ul><p>A member definition is experimental if</p><ul><li>It is annotated with @experimental</li><li>All overridden definitions are experimental</li><li>Its owner is an experimental class</li></ul><p>The annotation definition itself, class experimental is also experimental.</p><h3>Scala 3.0.2</h3><p>The <a href="https://github.com/lampepfl/dotty/releases/tag/3.0.2">Scala 3.0.2 release</a> continues with bug fixes and refinements. None of them impact the book, so I won’t discuss the details here. See the <a href="https://github.com/lampepfl/dotty/releases/tag/3.0.2">release notes</a> for more information.</p><h3>Scala 3.1.0-RC1</h3><p>The <a href="https://github.com/lampepfl/dotty/releases/tag/3.1.0-RC1">first release candidate for Scala 3.1.0</a> came out a few days ago. As you might expect, more significant changes are coming in the 3.1 minor release. Here are a few of the notable changes.</p><h4>Experimental Safer Exceptions</h4><p>One of the great things about Scala and the cutting-edge language research that goes into it, is the way that old problems can be revisited with a fresh perspective. The <a href="https://github.com/lampepfl/dotty/pull/11721">experimental <em>safer exceptions</em></a> is a good example.</p><p>From the beginning, Java has supported <em>checked exceptions,</em> where a method can’t throw any so-called of the checked exceptions unless the method signature explicit states it might throw them with a throws clause. The checked exception classes are all subclasses of Throwable, other than RuntimeException and its subclasses, and Error and its subclasses.</p><p>This is great for communicating to users what might happen if something fails and a value can’t be returned. Unfortunately, as designed, it forces all methods that call such methods to either catch and handle those exceptions or add corresponding throws clauses.</p><p>While you could argue that disciplined design would make this work, in practice most Java developers use subtypes of RuntimeException for custom exceptions and often catch the checked exceptions, then wrap them in unchecked exceptions, so they throw something without any pain…</p><p>In Scala, Either[A,B] and similar types are often returned, avoiding thrown exceptions altogether, to achieve the same goal of ensuring the method signature fully describes all possible outcomes (along with other benefits I won’t go into). However, sometimes throwing an exception is a good design choice. The new <em>safer exceptions</em> feature is designed for such situations, while still providing appropriate method signatures, but without the drawbacks of classic checked exceptions.</p><p>I won’t discuss all the details here. <a href="https://github.com/lampepfl/dotty/blob/b65958f139a0a818d36ee04468ada93b8fb472ac/docs/docs/reference/experimental/canthrow.md">The documentation page</a> is worth reading for all the details. However, here is the gist of how this feature works.</p><p>Using the documentation’s example, you first import language.experimental.saferExceptions to enable this feature. Now suppose you have the following code:</p><pre>val limit = 10e9<br>class LimitExceeded extends Exception<br>def f(x: Double): Double =<br>  if x &lt; limit then f(x) else throw LimitExceeded())</pre><p>With the feature enabled, f will fail to compile because The ability to throw exception LimitExceeded is missing. The message will also provide suggestions for how to fix this, including the following change:</p><pre>def f(x: Double): Double canThrow LimitExceeded =<br>  if x &lt; limit then f(x) else throw LimitExceeded())</pre><p>The new canThrow defines what exceptions can be thrown. The implementation will use compiler-generated given instances of a type CanThrow[E]. In this case, erased given ctl: CanThrow[LimitExceeded] = ???The erased keyword is another experimental feature that marks definitions for removal from the generated byte code. Using ??? as the definition doesn’t cause any problems, because this instance will never be used at runtime, so the ??? method will never be invoked! Hence, this given instance is effectively a “marker” to allow the throw LimitExceeded expression to compile.</p><p>The implementation also has the important benefit of solving the checked exception issue described above, namely that callers of this method do not need to declare types with canThrow. So, for example:</p><pre>@main def test(xs: Double*) =<br>  try println(xs.map(f).sum)<br>  catch case ex: LimitExceeded =&gt; println(&quot;too large&quot;)</pre><p>Here, our old friend println compiles just fine, as before, even though f can throw an exception.</p><p>There are a lot more details in the <a href="https://github.com/lampepfl/dotty/blob/b65958f139a0a818d36ee04468ada93b8fb472ac/docs/docs/reference/experimental/canthrow.md">documentation</a>, including support for migrating code to incrementally to use this feature.</p><h4>More Configurable Compiler Warnings</h4><p>A “regression” from Scala 2 is the ability to mark code with a @nowarn annotation, so the compiler doesn’t emit warnings for it and also the ability to configure warnings. Scala 3.1.0 will <a href="https://github.com/lampepfl/dotty/pull/12857">now support</a> a -Wconfoption for configuring warnings and it restores the <a href="http://twitter.com/nowarn">@nowarn</a> annotation.</p><h4>More Type Class instances for CanEqual</h4><p>Scala 3 introduces a new concept called <em>multiversal equality</em> that is implemented with a new CanEqual type class<em>. </em>I haven’t blogged about this feature yet, but I’ll do that as my next post. I’ll mention a few improvements Scala 3.1 introduces, namely more type class instances (givens) of CanEqual for common types. Stay tuned.</p><blockquote>See <a href="http://programming-scala.com/">Programming Scala, Third Edition</a> for more background information about most the features discussed in this post<em>.</em></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=be0830c059f5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/scala-3/scala-3-whats-changed-since-scala-3-0-0-be0830c059f5">Scala 3: What’s Changed Since Scala 3.0.0</a> was originally published in <a href="https://medium.com/scala-3">Scala 3</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Scala 3: Macros]]></title>
            <link>https://medium.com/scala-3/scala-3-macros-d63dd6811f89?source=rss----de7527e0301e---4</link>
            <guid isPermaLink="false">https://medium.com/p/d63dd6811f89</guid>
            <category><![CDATA[scala-3]]></category>
            <category><![CDATA[scala-programming]]></category>
            <category><![CDATA[scala]]></category>
            <dc:creator><![CDATA[Dean Wampler]]></dc:creator>
            <pubDate>Tue, 06 Jul 2021 00:04:41 GMT</pubDate>
            <atom:updated>2022-05-22T14:06:07.388Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AK6EucyY2DwV7bO8U0IuLw.jpeg" /><figcaption><a href="https://www.flickr.com/photos/deanwampler/51201464386/in/dateposted-public/">Six Vista Towers, Chicago</a> (from Navy Pier), © 2021, Dean Wampler</figcaption></figure><blockquote><strong>Update May 22, 2022:</strong> Michel Charpentier correctly pointed out that the arguments don’t need to be by-name if they are inlined. This makes perfect sense if you think about it <em>(which I didn’t 🤓)</em>, because we are no longer calling the <em>invariant</em> and <em>fail</em> functions; they are now gone, replaced by their bodies! I’ve updated the gist and the previous post accordingly. Thanks, Michel!</blockquote><p><a href="https://medium.com/scala-3/scala-3-a-look-at-inline-and-programming-scala-is-now-published-9690ca43c23a">Last time</a> I introduced one of the new metaprogramming features in Scala 3, the inline keyword and how it affects source code. <a href="https://gist.github.com/deanwampler/466e8ce52ae21b48a0fb8c60aa595076">The example I used</a> also contained a macro definition, using <em>quoting</em> and <em>splicing. </em>This post introduces those concepts.</p><blockquote><a href="http://programming-scala.com/">Programming Scala, Third Edition</a> is now available. It provides a comprehensive introduction to Scala 3 changes for experienced Scala developers, as well as a complete introduction to Scala for new Scala developers.</blockquote><p>Here is the previous example, which ensures that an invariant holds before and after a block of code is executed.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5e81807035631920f19f0ebfefa5e233/href">https://medium.com/media/5e81807035631920f19f0ebfefa5e233/href</a></iframe><p>Quotation and splicing are the key components of compile-time <em>macros</em> in Scala 3, which is a new system replacing the experimental system in Scala 2.</p><p><em>Quotation</em> is where sections of source code are converted to a tree-like data structure of type Expr[T], where T corresponds to the expression’s type. A quotation of an expression uses the syntax &#39;{...}, with the source code to quote inside the braces. For types, &#39;[...] is used.</p><p><em>Splicing</em> goes the other direction, converting quotations back to source code. Splices are expressed using ${...}, which deliberate draws a parallel with string interpolation.</p><p>Both are used together with inline to construct code programmatically at compile time. There is also a <em>runtime staging </em>capability, which uses quotes and splices, but not inline, to construct code at runtime instead of compile time. I’ll explore runtime staging in a subsequent post.</p><p><a href="https://medium.com/scala-3/scala-3-a-look-at-inline-and-programming-scala-is-now-published-9690ca43c23a">Last time</a> I explained what the InvariantEnabled.apply method is doing and how inline determines what byte code actually gets generated, depending on compile-time analysis of the ignore field, etc.</p><p>If the predicate evaluates to false, the private method fail is called, which uses a splice ${...} to construct the correct source code from the Expr[String] returned by failImpl. An Expr[T] encapsulates the “tree-like data structure” mentioned above.</p><p>When fail has finished its work, new source code will have been inserted into the compilation stream for the compiler to compile. Using inline allows quoting and splicing to happen at compile time.</p><p>Within a quote or splice, identifiers can be quoted or spliced, such as the arguments ‘predicate, ‘message, ‘block, ‘beforeAfter that are passed to failImpl. Note the types of the arguments received by failImpl. Each is an Expr[T] for some T. So, for example, writing ‘predicate converts the by-name Boolean predicate into an Expr[Boolean]. There is also a using scala.quotes.Quotes instance required by failImpl.</p><p>Notice what happens in the body of failImpl. Since we are returning an Expr[String], the body consists of ‘{...}. Inside the braces is code to construct and throw the InvariantFailure exception.</p><p>Most of work is in the construction of the message string. In ${showExpr(predicate)}, the helper method showExpr effectively returns the string representation for the input Expr[Boolean] as an Expr[String]. What this means practically is the source code for the expression is captured as a string. The returned Expr[String] is then spliced into the larger message string with the outer set of ${}. The other input parameters are evaluated similarly, except for beforeAndAfter, which doesn’t need to be passed through showExpr, because it is already an Expr[String].</p><p>Recall from the <a href="https://medium.com/scala-3/scala-3-a-look-at-inline-and-programming-scala-is-now-published-9690ca43c23a">previous post</a> that the point of all this is to create an error message string with the actual source code that failed the invariant check. We also saw in the example we used that the code had already been converted from operator notation when the string representation was generated by the macro. For example, thing1.label != “label&quot; became thing1.label.!=(“label”) and i * 2 % 3 became i.*(2).%(3). Not perfect, but good enough for debugging failures.</p><p>For an overview of Scala 3 metaprogramming, see <a href="https://dotty.epfl.ch/docs/reference/metaprogramming/toc.html">this Dotty documentation</a>, which also links to more detailed information about macros.</p><blockquote>See <a href="http://programming-scala.com/">Programming Scala, Third Edition</a> for more information about the new metaprogramming facilities and Scala 3, in general.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d63dd6811f89" width="1" height="1" alt=""><hr><p><a href="https://medium.com/scala-3/scala-3-macros-d63dd6811f89">Scala 3: Macros</a> was originally published in <a href="https://medium.com/scala-3">Scala 3</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Scala 3: A Look at “inline” (and “Programming Scala” is Now Published!)]]></title>
            <link>https://medium.com/scala-3/scala-3-a-look-at-inline-and-programming-scala-is-now-published-9690ca43c23a?source=rss----de7527e0301e---4</link>
            <guid isPermaLink="false">https://medium.com/p/9690ca43c23a</guid>
            <category><![CDATA[scala-3]]></category>
            <category><![CDATA[scala-programming]]></category>
            <category><![CDATA[scala]]></category>
            <dc:creator><![CDATA[Dean Wampler]]></dc:creator>
            <pubDate>Sun, 27 Jun 2021 17:14:03 GMT</pubDate>
            <atom:updated>2022-05-22T14:03:34.533Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/595/1*FvnO4KpyBgPoJANOycMwkg.png" /><figcaption>From <a href="https://twitter.com/tpolecat/status/1408951290915328002">Teh Interwebs</a>…</figcaption></figure><blockquote><strong>Update April 3, 2022: </strong>I added the code examples below to the book’s code repo and did more extensive testing. See <a href="https://github.com/deanwampler/programming-scala-book-code-examples/tree/master/src/main/scala/progscala3/meta/performance">here</a> for details.</blockquote><blockquote><strong>Update May 22, 2022:</strong> Michel Charpentier correctly pointed out that the arguments don’t need to be by-name if they are inlined. This makes perfect sense if you think about it (which I didn’t <em>🤓</em>), because we are no long calling the <em>invariant</em> and fail functions; they are now gone, replaced by their bodies! I’ve updated the gist and the text accordingly. Thanks, Michel!</blockquote><p>I haven’t blogged yet about the <a href="https://docs.scala-lang.org/scala3/guides/macros/index.html">new metaprogramming system in Scala 3</a>, so let’s start that now. First, let’s look at the new inline keyword, which causes the compiler to “inline” the decorated code.</p><blockquote><a href="http://programming-scala.com/">Programming Scala, Third Edition</a> is now published! It provides a comprehensive introduction to all the new features in Scala 3, while also introducing the Scala 2 features you’ll still need to know for working with an existing code base. <a href="http://programming-scala.com/">Programming Scala, Third Edition</a> is aimed at experienced Scala developers who want to learn what’s new, as well as professional developers getting started with Scala.</blockquote><p>“Inlining” means that instead of generating the usual byte code for a construct, like a conditional, val declaration, or method, the compiler inserts byte code that bypasses the overhead of the construct.</p><p>For a conditional, instead of doing the usual if (predicate) true_stuff else false_stuff, the compiler just inserts true_stuff if predicate is determined to be true <em>at compile time</em> or it inserts false_stuff if predicate is determined to be false. Hence, inline can’t inline conditionals when the value of predicate can’t be determined at compile time. I’ll show you an example in a moment.</p><p>For a val, the actual value, which must also be known at compile time, is inserted everywhere a reference to the val is made.</p><p>For a method, the body is inlined instead of calling the method. This could add code bloat for big methods used in many places, so only inline small methods. You won’t gain much runtime performance inlining large methods anyway.</p><p>The method arguments (which can also be inlined) don’t have to be compile-time constants, but if there are type parameters (e.g., def foo[T](t:T): Unit), the actual types have to be known at compile time, where the method is “called”.</p><p>As an example, suppose I need an <em>invariant checker</em>, a tool that allows me to specify some invariant that should be true before and after some code executes. Here is a possible implement using the Scala 3 metaprogramming tools:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5e81807035631920f19f0ebfefa5e233/href">https://medium.com/media/5e81807035631920f19f0ebfefa5e233/href</a></iframe><p>Note the inline keywords. I start by importing scala.quoted.*, then define an object to implement the invariant checker.</p><p>First, I inline a flag ignore, which specifies whether or not to “ignore” invariant checking. This is analogous to how some of the assert related Scala library methods worked in Scala 2, where you could disable them at runtime by passing certain flags to scala. (At this time, this feature hasn’t been implemented in Scala 3.)</p><p>It would be convenient for the user to make this value a var, so it can be changed dynamically at runtime, without recompilation. However, this would prevent inlining, so if you want to disable the runtime checks, you have to recompile with the value set to true instead.</p><p>If you’re playing along at home, try adding the type annotation :Boolean to the declaration. You get this error:</p><pre>[error] -- Error: .../InvariantEnabled.scala:5:21<br>[error] 6 |  inline val ignore: Boolean = false<br>[error]   |                     ^^^^^^^<br>[error]   |                     inline value must have a literal constant type</pre><p>The problem is that a Boolean can have two values, but the compiler only accepts the <em>literal type constant</em> true or false here, not Boolean. So, you could use false as the type here:</p><pre>inline val ignore: false = false</pre><p>Or, just leave off the type annotation. See my post, <a href="https://medium.com/scala-3/scala-3-dependent-types-part-i-273e9076e8f8">Scala 3: Dependent Types, Part I</a> for more details on <em>dependent types</em>.</p><p>I won’t show it here, but I also defined a nearly-identical type, <a href="https://gist.github.com/deanwampler/3a1c2b0ea9da7edf9f971b601d3a5eb2">InvariantDisabled</a>, where the only difference is to declare ignore to be true. I’ll use both of these types in an example below.</p><p>Moving on, apply and all of its parameters are defined inline. This method, along with fail, need to be inline for the new macro <em>quoting</em> and <em>splicing </em>to work. I’ll discuss those features in a subsequent post. For our purposes now, declaring these methods inline means that the byte code won’t contain calls to methods with these names, but instead it will contain their bodies inserted inline.</p><p>Similarly, the parameters predicate, message, and block will be inlined.</p><p>Now we come to the conditional, inline if ignore then ... else ... <em>If </em>ignore is true, then the byte code for the following four lines will be inserted:</p><pre>if !predicate then fail(predicate, message, block, &quot;before&quot;)      <br>val result = block<br>if !predicate then fail(predicate, message, block, &quot;after&quot;)      <br>result</pre><p>fail is also inlined, so the actual byte code will contain the call to failImpl, which constructs and throws an InvariantFailure.</p><p>But what if ignore is false at compile time? Then only the byte code for block is inserted. Hence, <em>there will be no runtime overhead for unused invariant checking!</em> We’ll see this in action shortly.</p><p>I’ll discuss the rest of this example in a subsequent post that explores <em>quoting </em>and <em>splicing.</em></p><p>Finally, here is another variant that removes most of the inlining, except where needed for quoting and splicing, but still performs invariant checking:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dbe0248c548843c3f71665a22bd63850/href">https://medium.com/media/dbe0248c548843c3f71665a22bd63850/href</a></iframe><p>Note that now I need to pass the arguments predicate, message, and block as by-name parameters, so they are only evaluated inside the method bodies for apply and fail, not <em>before</em> calling those methods. This is not necessary when these arguments are inlined! However, even though this implementation still does invariant checking, not inlining the arguments means we won’t get the same expressions output as strings in the error messages. For example, instead of FAILURE! predicate “i.&gt;=(0)” you get less useful output like FAILURE! predicate “predicate$proxy6”.</p><p>So, I claimed that a major advantage of inlining is the ability to remove whole blocks of unneeded code, <em>if </em>that situation can be determined at compile time. The inlined val ignore triggers this situation. Now let’s see what sort of performance impact this has. Consider the following program:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e77b9ead1ccefa742f4358f4ac1d8c4a/href">https://medium.com/media/e77b9ead1ccefa742f4358f4ac1d8c4a/href</a></iframe><p>This program accepts zero or more numbers for the number of trials to run. If none is specified, it defaults to 1000. For each n, the program times the execution using invariantEnabled, invariantDisabled, and invariantNoInline, then prints out the times in nanoseconds and the percentages vs. what should be the fastest execution times, those for invariantDisabled.</p><p>Running this program with the arguments 10 100 1000 10000 100000, results in the following:</p><pre>|      N |                    Elapsed Times (nanos)             |<br>|        |  Enabled | Disabled |   E/D%   | NoInline |   N/D%   |<br>|+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|<br>|     10 |   329267 |   140683 |  234.05% |   397920 |  282.85% |<br>|    100 |   117403 |    33356 |  351.97% |    87826 |  263.30% |<br>|   1000 |   402782 |   152507 |  264.11% |   532264 |  349.01% |<br>|  10000 |   882765 |   220922 |  399.58% |  1941655 |  878.89% |<br>| 100000 |  1288324 |  1140562 |  112.96% |  1555927 |  136.42% |<br>|+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|</pre><p>The numbers can vary quite a lot from run to run, especially for larger N where JVM hotspot<em> </em>optimization kicks in. However, the general trend is clear at least for smaller N; <em>compiling</em> with checking disabled eliminates significant overhead and so does extensive inlining.</p><p>By the way, what happens if a check fails. If you change == to != in line 11 of InlinePerf, you’ll get this error:</p><pre>[error] ...invariantEnabled$InvariantFailure: FAILURE! predicate &quot;thing1.label.!=(&quot;label&quot;)&quot; failed before evaluation of block: &quot;thing1.count = i.*(2).%(3)&quot;. Message = &quot;&quot;.</pre><p>We see an important benefit of using a macro implementation; we can compose an error message that shows the actual code for both the predicate and the block that triggered the failure. Note, however, that the <em>operator notation</em> is converted to method invocations. Still, this is very handy when debugging.</p><p>Also, recall I said that the method parameters don’t have to be compile time constants, even though we inline them and the method. Note that the predicate we inlined is thing1.label == &quot;label&quot; and the block changes the value of thing1.count, neither of which is constant at compile time.</p><p>You can get carried away with inlining (like anything else). Outside the context of macros, it’s best to profile your code to determine if a) you really need to improve the performance of some section of code and b) using inline actually makes a significant difference in real-world execution scenarios (recall the behavior for large N above…).</p><p>So for example, I gave up the convenience of switching invariant checking on and off at runtime by inlining the ignore value. The performance gains were noticeable, but do they outweigh the convenience of the runtime flexibility?</p><blockquote>See <a href="http://programming-scala.com/">Programming Scala, Third Edition</a> for more information about the new metaprogramming facilities and Scala 3, in general.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9690ca43c23a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/scala-3/scala-3-a-look-at-inline-and-programming-scala-is-now-published-9690ca43c23a">Scala 3: A Look at “inline” (and “Programming Scala” is Now Published!)</a> was originally published in <a href="https://medium.com/scala-3">Scala 3</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Scala 3: It’s Here!]]></title>
            <link>https://medium.com/scala-3/scala-3-its-here-e85905638025?source=rss----de7527e0301e---4</link>
            <guid isPermaLink="false">https://medium.com/p/e85905638025</guid>
            <category><![CDATA[scala]]></category>
            <category><![CDATA[scala-3]]></category>
            <category><![CDATA[scala-programming]]></category>
            <dc:creator><![CDATA[Dean Wampler]]></dc:creator>
            <pubDate>Fri, 14 May 2021 13:10:01 GMT</pubDate>
            <atom:updated>2021-05-14T13:10:01.489Z</atom:updated>
            <content:encoded><![CDATA[<p>Scala 3.0.0 is now final!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RWrpwzaktsf1FlhMDRbD9Q.jpeg" /><figcaption><a href="https://www.flickr.com/photos/deanwampler/51178192776/">After 8</a>, © 2020, Dean Wampler</figcaption></figure><p>After 8+ years of effort, Scala 3 was officially released this week. This <a href="https://scala-lang.org/blog/2021/05/14/scala3-is-here.html">blog post</a> on the Scala language website provides some details, along with a cool visualization of the evolving git tree over that time.</p><p><a href="http://programming-scala.com/">Programming Scala, Third Edition</a> is almost finished, too. We’re in the final production steps with a planned ebook release early in June. Print copies will be available a few weeks later.</p><p>Looking for the perfect wedding gift this Spring? You’re welcome! You can preorder on <a href="https://www.amazon.com/Programming-Scala-Scalability-Functional-Objects/dp/1492077895/ref=sr_1_1">Amazon</a>, <a href="https://www.powells.com/book/programming-scala-9781492077893">Powells</a>, and other vendors. The <a href="http://programming-scala.com/">book page</a> has more links.</p><p>I’ll return with more posts about Scala 3 features soon.</p><blockquote><em>For a concise summary of most of the notable changes in Scala 3, see my new </em><a href="https://deanwampler.github.io/scala3-highlights.html"><em>Scala 3 Highlights</em></a><em> page.</em></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e85905638025" width="1" height="1" alt=""><hr><p><a href="https://medium.com/scala-3/scala-3-its-here-e85905638025">Scala 3: It’s Here!</a> was originally published in <a href="https://medium.com/scala-3">Scala 3</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Scala 3: Safer Pattern Matching with Matchable]]></title>
            <link>https://medium.com/scala-3/scala-3-safer-pattern-matching-with-matchable-f0396430ded6?source=rss----de7527e0301e---4</link>
            <guid isPermaLink="false">https://medium.com/p/f0396430ded6</guid>
            <category><![CDATA[scala-programming]]></category>
            <category><![CDATA[scala-3]]></category>
            <category><![CDATA[scala]]></category>
            <dc:creator><![CDATA[Dean Wampler]]></dc:creator>
            <pubDate>Wed, 28 Apr 2021 15:57:48 GMT</pubDate>
            <atom:updated>2021-04-28T15:57:47.934Z</atom:updated>
            <content:encoded><![CDATA[<p>Late in the Scala 3 development process, a new type scala.Matchable was introduced to fix a loophole in pattern matching. This post discusses that loophole, how Matchable fixes it, and implications of this change.</p><figure><a href="https://www.flickr.com/photos/deanwampler/51095530521/"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3uW87SlbELGr5KD2eVKn7g.jpeg" /></a><figcaption><a href="https://www.flickr.com/photos/deanwampler/51095530521/">Lincoln Park Education Pavilion Detail</a>, © 2021, Dean Wampler</figcaption></figure><blockquote><em>For an even more concise summary of most of the notable changes in Scala 3, see my new </em><a href="https://deanwampler.github.io/scala3-highlights.html"><em>Scala 3 Highlights</em></a><em> page.</em></blockquote><blockquote><em>Like Scala 3 itself, </em><a href="http://programming-scala.org/"><em>Programming Scala, Third Edition</em></a><em> is almost finished! You can read a draft of it on the </em><a href="https://www.oreilly.com/library/view/programming-scala-3rd/9781492077886/"><em>O’Reilly Learning Platform</em></a><em>.</em></blockquote><p>Arrays in Scala have always been Java arrays, which are mutable. Scala 3 introduces a new immutable wrapper around arrays call <a href="https://dotty.epfl.ch/api/scala/IArray$.html">IArray</a>, which disallows modification, but pattern matching caused a hole:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bbc7a8d47d56b69b48b680490c1e3467/href">https://medium.com/media/bbc7a8d47d56b69b48b680490c1e3467/href</a></iframe><p>Running this code in the Scala 3 REPL returns this output for a supposedly immutable array wrapper:</p><pre>val iarray: opaques.IArray[Int] = Array(1, 2, 300, 4, 5)</pre><p>However, if you invoke the compiler or REPL with the flag -source:future (which I recommend, once you’ve transitioned your code base from Scala 2), you get a warning, too:</p><pre>val iarray: IArray[Int] = Array(1, 2, 300, 4, 5)<br>3 |  case a: Array[Int] =&gt; a(2) = 300   // Scala 3 warning!!<br>  |          ^^^^^^^^^^<br>  |          pattern selector should be an instance of Matchable,<br>  |          but it has unmatchable type IArray[Int] instead</pre><p>scala.Matchable is a new <em>universal</em> trait introduced into Scala’s type hierarchy that is designed to be a parent type of all allowed pattern matching expressions. Matchable&#39;s parent is Any, so it is a new sibling of AnyVal and AnyRef, the first one introduced since the early days of Scala! Both AnyVal and AnyRef are subtypes of Matchable, so concrete values and reference types will continue to work in pattern matching expressions without change.</p><p>However, this is is a breaking change, as I’ll demonstrate below, which is why the -source:future flag is required in Scala 3.0 to trigger the warning about the need for Matchable.</p><p>So, how does Matchable help in this situation involving IArray? Recall in <a href="https://medium.com/scala-3/opaque-type-aliases-and-open-classes-13076a6c07e4">Scala 3: Opaque Type Aliases and Open Classes</a>, I discussed the new <em>opaque type aliases </em>feature<em>.</em> IArray is declared as an opaque type alias:</p><pre>opaque type IArray[+T] = Array[_ &lt;: T]</pre><p>IArray, like all opaque types, is considered an <em>abstract</em> type by the compiler, not concrete, and it is not explicitly declared as a subtype of Matchable, which is why we get the warning when -source:future is used.</p><p>By the way, I mentioned that Matchable is a <em>universal</em> trait, which are traits with the following properties:</p><ul><li>They subtype Any, but not any other universal traits.</li><li>They define only methods.</li><li>They do no initialization of their own.</li></ul><p>Universal traits are important for defining <em>value classes</em>, which were also discussed in my <a href="https://medium.com/scala-3/opaque-type-aliases-and-open-classes-13076a6c07e4">post on opaque type aliases</a><em>. </em>(Both universal traits and value classes are Scala 2 features.)</p><h4>Consequences of Matchable</h4><p>While pattern matching for concrete values and reference types will continue work without change, the <a href="https://dotty.epfl.ch/docs/reference/other-new-features/matchable.html">Dotty page for </a><a href="https://dotty.epfl.ch/docs/reference/other-new-features/matchable.html">Matchable</a> says that a pattern-matching clause will trigger a warning for the following cases:</p><ul><li>Type Any: if pattern matching is required one should use Matchable instead.</li><li>Unbounded type parameters and abstract types: If pattern matching is required they should have an upper bound Matchable.</li><li>Type parameters and abstract types that are only bounded by some universal trait: Again, Matchable should be added as a bound.</li></ul><p>The second and third bullets will impact a lot of code you write. Consider the following two implementations of an examine method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dd38bd0ed515934a5f1b9812edf86ec4/href">https://medium.com/media/dd38bd0ed515934a5f1b9812edf86ec4/href</a></iframe><p>With -source:future, examine1 will throw this warning:</p><pre>2 |  case i: Int =&gt; s&quot;Int: $i&quot;<br>  |          ^^^<br>  |          pattern selector should be an instance of Matchable,<br>  |          but it has unmatchable type T instead</pre><p>Previously, this was perfectly acceptable Scala code. The correct implementation now uses the type bound shown for examine2. Hence, in a future release of Scala 3 that makes this warning the default behavior, you’ll need to modify all such pattern matching expressions that involve abstract types or type parameters, like this example.</p><p>You’ll also encounter situations where Matchable will now be inferred as the <em>least upper bound</em>, instead of Any, which would have been inferred previously:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a482e8aa00777fda832456d0c718fdf7/href">https://medium.com/media/a482e8aa00777fda832456d0c718fdf7/href</a></iframe><p>In Scala 2, seq would have type Seq[Any]. Similarly, in the last case clause shown, case unexpected, the type of unexpected will be inferred to be Matchable, not Any, as it would be in Scala 2.</p><p>Finally, Scala 3&#39;s support for <em>dependently-typed methods</em> is impacted by this change. Recall that I discussed dependent typing in two posts, <a href="https://medium.com/scala-3/scala-3-dependent-types-part-i-273e9076e8f8">part 1</a> and <a href="https://medium.com/scala-3/scala-3-dependent-types-part-ii-e7fc04dbfb08">part 2</a>, but I didn’t discuss dependently-typed methods in those posts.</p><p>Here is an example of a dependent type declaration and a dependently-typed method that uses it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b1992c546d1f1f3606c14383ee9df4e6/href">https://medium.com/media/b1992c546d1f1f3606c14383ee9df4e6/href</a></iframe><p>First, ElemR is a recursive <em>match type</em> alias that returns the actual type used for the type parameter of a <em>higher-kinded type’s.</em> For example, for Seq[Foo], it will return type Foo. See the <em>Appendix</em> in the <a href="https://medium.com/scala-3/scala-3-dependent-types-part-ii-e7fc04dbfb08">dependent typing part 2 post</a> for more details on <em>match types</em>.</p><p>ElemR is used by first, a <em>dependently-typed method</em> that returns the first element in a “container” or just the item itself for other instances. Note the return type ElemR means the actual type of the instance returned will be used, not Any or similar. In other words, the method’s return type is dependent on the instance, which is how <em>dependently-typed methods</em> get their name. The examples at the end show what happens. Note the specific types shown for returned values.</p><p>If you replace x.asMatchable with x, you’ll get the same type warnings we saw previously, even though the definition of ElemR doesn’t trigger this complaint itself.</p><p>The alternative to using asMatchable is to change the type parameter for first to be[X &lt;: Matchable]. Then you can use x match ... in the body without problems.</p><p>For more on Matchable, see the <a href="https://dotty.epfl.ch/docs/reference/other-new-features/matchable.html">Dotty documentation for it</a>, which also discusses the implications for equality checking and how some Any methods might be moved to Matchable in a future Scala release.</p><blockquote><em>For an even more concise summary of most of the notable changes in Scala 3, see my new </em><a href="https://deanwampler.github.io/scala3-highlights.html"><em>Scala 3 Highlights</em></a><em> page.</em></blockquote><blockquote><em>Like Scala 3 itself, </em><a href="http://programming-scala.org/"><em>Programming Scala, Third Edition</em></a><em> is almost finished! You can read a draft of it on the </em><a href="https://www.oreilly.com/library/view/programming-scala-3rd/9781492077886/"><em>O’Reilly Learning Platform</em></a><em>.</em></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f0396430ded6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/scala-3/scala-3-safer-pattern-matching-with-matchable-f0396430ded6">Scala 3: Safer Pattern Matching with Matchable</a> was originally published in <a href="https://medium.com/scala-3">Scala 3</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>