<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="assets/xml/rss.xsl" media="all"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Solid Ninjas Blogging</title><link>https://blog.solidninja.is/</link><description>Blog for Vladimir Lushnikov (previously SOLID Ninja Ltd.)</description><atom:link href="https://blog.solidninja.is/rss.xml" rel="self" type="application/rss+xml"></atom:link><language>en</language><lastBuildDate>Wed, 07 Aug 2019 14:58:09 GMT</lastBuildDate><generator>Nikola (getnikola.com)</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Why Scala?</title><link>https://blog.solidninja.is/posts/why-scala/</link><dc:creator>Vladimir Lushnikov</dc:creator><description>&lt;div&gt;&lt;div class="contents topic" id="contents"&gt;
&lt;p class="topic-title first"&gt;Contents&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference internal" href="https://blog.solidninja.is/posts/why-scala/#why-am-i-writing-this" id="id3"&gt;Why am I writing this?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference internal" href="https://blog.solidninja.is/posts/why-scala/#tl-dr" id="id4"&gt;TL;DR;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference internal" href="https://blog.solidninja.is/posts/why-scala/#language-background" id="id5"&gt;Language background&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference internal" href="https://blog.solidninja.is/posts/why-scala/#what-features-in-scala-help-the-most" id="id6"&gt;What features in Scala help the most?&lt;/a&gt;&lt;ul&gt;
&lt;li&gt;&lt;a class="reference internal" href="https://blog.solidninja.is/posts/why-scala/#id1" id="id7"&gt;Case classes&lt;/a&gt;&lt;ul&gt;
&lt;li&gt;&lt;a class="reference internal" href="https://blog.solidninja.is/posts/why-scala/#and-algebraic-data-types" id="id8"&gt;and Algebraic Data Types&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a class="reference internal" href="https://blog.solidninja.is/posts/why-scala/#pattern-matching" id="id9"&gt;Pattern matching&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference internal" href="https://blog.solidninja.is/posts/why-scala/#implicits-given" id="id10"&gt;Implicits/given&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference internal" href="https://blog.solidninja.is/posts/why-scala/#macros" id="id11"&gt;Macros&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a class="reference internal" href="https://blog.solidninja.is/posts/why-scala/#summary" id="id12"&gt;Summary&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="why-am-i-writing-this"&gt;
&lt;h2&gt;&lt;a class="toc-backref" href="https://blog.solidninja.is/posts/why-scala/#id3"&gt;Why am I writing this?&lt;/a&gt;&lt;/h2&gt;
&lt;img alt="hand drill" class="align-left" src="https://blog.solidninja.is/images/24521934956_3e117a9d31_b.jpg" style="width: 300px;"&gt;
&lt;p&gt;Clients, recruiters and fellow software engineers usually ask what makes it "better" than the incumbent industry language - &lt;a class="reference external" href="https://www.java.com"&gt;Java&lt;/a&gt;. I get a lot of questions from people that perhaps have not encountered &lt;a class="reference external" href="https://www.scala-lang.org"&gt;Scala&lt;/a&gt; before asking why they would use it over something that &lt;em&gt;most&lt;/em&gt; people (for some definition of most that probably includes Computer Science graduates) are already familiar with. To make the (hopefully only) analogy - if you have a hand drill that works, why would you want a power tool? One answer could be scale and convenience - you can build a fine piece of furniture with a hand drill, but if you need to make many holes in a concrete wall, it is much more practical to use a power drill with a hammer function.&lt;/p&gt;
&lt;p&gt;This article will attempt to explain why I prefer Scala over Java for most tasks where one could pick either. The usual disclaimer applies - it's just one (albeit very powerful) set of tools in the programmer's toolbox, and will not help you be better at anything if you use it incorrectly (such as drilling in the wrong place or with the wrong technique. Ok, &lt;tt class="docutils literal"&gt;analogy_count++&lt;/tt&gt;). I will start at a very basic technical level and increase the level of detail as the article continues.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="tl-dr"&gt;
&lt;h2&gt;&lt;a class="toc-backref" href="https://blog.solidninja.is/posts/why-scala/#id4"&gt;TL;DR;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Scala can be a very productive language to develop in - a business domain can be modelled consisely (using &lt;span class="target" id="case-classes"&gt;case classes&lt;/span&gt;), complex problems can be broken down into simpler pieces (using &lt;span class="target" id="function-composition"&gt;function composition&lt;/span&gt; and &lt;a class="reference external" href="https://docs.scala-lang.org/tour/pattern-matching.html"&gt;pattern matching&lt;/a&gt;) and some classes of errors can even be eliminated at compile time (using &lt;span class="target" id="typeclass-derivation"&gt;typeclass derivation&lt;/span&gt; as an example). It is indeed a more complicated language than Java but overall I think the gains are worth the initial ramp-up time.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="language-background"&gt;
&lt;h2&gt;&lt;a class="toc-backref" href="https://blog.solidninja.is/posts/why-scala/#id5"&gt;Language background&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;As an introduction - &lt;a class="reference external" href="https://www.java.com"&gt;Java&lt;/a&gt; is a &lt;a class="reference external" href="https://en.wikipedia.org/wiki/General-purpose_language"&gt;general-purpose&lt;/a&gt; &lt;a class="reference external" href="https://en.wikipedia.org/wiki/Object-oriented_programming"&gt;object-oriented&lt;/a&gt; programming language first released in 1995, with a static type system that includes &lt;a class="reference external" href="https://en.wikipedia.org/wiki/Parametric_polymorphism"&gt;parametric polymorphism&lt;/a&gt; and &lt;a class="reference external" href="https://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)"&gt;inheritance&lt;/a&gt;, targeting a runtime system with &lt;a class="reference external" href="https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)"&gt;garbage collection&lt;/a&gt; and a bytecode VM (the Java Virtual Machine or &lt;a class="reference external" href="https://en.wikipedia.org/wiki/Java_virtual_machine"&gt;JVM&lt;/a&gt;). &lt;a class="reference external" href="https://www.scala-lang.org"&gt;Scala&lt;/a&gt; is a &lt;a class="reference external" href="https://en.wikipedia.org/wiki/Programming_paradigm"&gt;multi-paradigm&lt;/a&gt; &lt;a class="reference external" href="https://en.wikipedia.org/wiki/General-purpose_language"&gt;general-purpose&lt;/a&gt; programming language (&lt;a class="reference external" href="https://en.wikipedia.org/wiki/Object-oriented_programming"&gt;object-oriented&lt;/a&gt; and &lt;a class="reference external" href="https://en.wikipedia.org/wiki/Functional_programming"&gt;functional&lt;/a&gt;) first released in 2004, with a static type system (that has all the features of Java's and more), targeting primarily the JVM, but also the web (with &lt;a class="reference external" href="https://www.scala-js.org"&gt;Scala.js&lt;/a&gt;) and native code (with &lt;a class="reference external" href="https://www.scala-native.org"&gt;Scala Native&lt;/a&gt;). It is telling that IDEs offer support for automatic conversion of Java to Scala but not the other way round.&lt;/p&gt;
&lt;p&gt;The above paragraph contained quite a bit of jargon - let me try to define some of the terms:&lt;/p&gt;
&lt;dl class="docutils"&gt;
&lt;dt&gt;functional:&lt;/dt&gt;
&lt;dd&gt;ability to treat functions as a first-class value; also the general term for programming techniques grouped around that idea, such as: immutability, parametricity, currying&lt;/dd&gt;
&lt;dt&gt;inheritance:&lt;/dt&gt;
&lt;dd&gt;ability to get the behaviour and data (methods and fields) of parent class (superclass) by a single syntactic keyword (usually &lt;cite&gt;extends&lt;/cite&gt;)&lt;/dd&gt;
&lt;dt&gt;garbage collection:&lt;/dt&gt;
&lt;dd&gt;memory is allocated dynamically at runtime and is freed by collection at some later point dynamically determined at runtime (in contrast with &lt;em&gt;statically allocated&lt;/em&gt; memory where both the lifetime and sizes are known at compile time)&lt;/dd&gt;
&lt;dt&gt;general-purpose&lt;/dt&gt;
&lt;dd&gt;equally applicable to all domains (i.e. nothing specific to make video games vs. text editors for example)&lt;/dd&gt;
&lt;dt&gt;object-oriented:&lt;/dt&gt;
&lt;dd&gt;able to associate data (fields) with code (methods), usually using a convenient syntax&lt;/dd&gt;
&lt;dt&gt;parametric polymorphism:&lt;/dt&gt;
&lt;dd&gt;able to write a function that can use the same code to perform an operation across different types (like an addition operator of a semigroup e.g. the natural numbers)&lt;/dd&gt;
&lt;dt&gt;statically typed:&lt;/dt&gt;
&lt;dd&gt;every value has a specific type known at compile-time (e.g. you can differentiate between a integer and a piece of text)&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;Terms that will be used later are not going to be explained but only linked to definitions or examples elsewhere (to reduce the scope of the article).&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="what-features-in-scala-help-the-most"&gt;
&lt;h2&gt;&lt;a class="toc-backref" href="https://blog.solidninja.is/posts/why-scala/#id6"&gt;What features in Scala help the most?&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;As long as two languages are &lt;a class="reference external" href="https://en.wikipedia.org/wiki/Turing_completeness"&gt;Turing Complete&lt;/a&gt; any programs written in one language can be written in another. However, this is not very useful - it may be far more appropriate to write a program in &lt;cite&gt;C&lt;/cite&gt; vs. a program in &lt;cite&gt;Javascript&lt;/cite&gt; depending on factors like tooling, ecosystem support, the environment where the program is going to run, the amount of resources available to the program etc.&lt;/p&gt;
&lt;p&gt;Both languages run on the JVM, and both are used for backend development in small to large companies. To usefully compare Scala to Java we need to talk about specific features, my selection of which is:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;case classes&lt;/li&gt;
&lt;li&gt;pattern matching&lt;/li&gt;
&lt;li&gt;implicits (&lt;cite&gt;given&lt;/cite&gt; in Scala 3)&lt;/li&gt;
&lt;li&gt;macros (mostly library usages though)&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="section" id="id1"&gt;
&lt;h3&gt;&lt;a class="toc-backref" href="https://blog.solidninja.is/posts/why-scala/#id7"&gt;Case classes&lt;/a&gt;&lt;/h3&gt;
&lt;div class="section" id="and-algebraic-data-types"&gt;
&lt;h4&gt;&lt;a class="toc-backref" href="https://blog.solidninja.is/posts/why-scala/#id8"&gt;and Algebraic Data Types&lt;/a&gt;&lt;/h4&gt;
&lt;p&gt;A &lt;a class="reference external" href="https://docs.scala-lang.org/tour/case-classes.html"&gt;case class&lt;/a&gt; is a container for immutable fields and (optionally) other behavior. (In Java terminology, they are &lt;a class="reference external" href="https://docs.oracle.com/javase/8/docs/api/java/lang/doc-files/ValueBased.html"&gt;value-based classes&lt;/a&gt;). For example, suppose you have a bus position you want to represent that contains the following information:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;bus identifier (and maybe line, initial departure time, vehicle id)&lt;/li&gt;
&lt;li&gt;coordinates of bus&lt;/li&gt;
&lt;li&gt;timestamp&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You might initially model everything using &lt;tt class="docutils literal"&gt;String&lt;/tt&gt; (example in JSON):&lt;/p&gt;
&lt;pre class="code json"&gt;&lt;a name="rest_code_b5ab26dfae2e4816ad138a823a04a16b-1"&gt;&lt;/a&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;a name="rest_code_b5ab26dfae2e4816ad138a823a04a16b-2"&gt;&lt;/a&gt;    &lt;span class="nt"&gt;"bus"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"42"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;a name="rest_code_b5ab26dfae2e4816ad138a823a04a16b-3"&gt;&lt;/a&gt;    &lt;span class="nt"&gt;"coordinates"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"0.0,0.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;a name="rest_code_b5ab26dfae2e4816ad138a823a04a16b-4"&gt;&lt;/a&gt;    &lt;span class="nt"&gt;"timestamp"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"2019-01-01T12:23:22.813Z"&lt;/span&gt;
&lt;a name="rest_code_b5ab26dfae2e4816ad138a823a04a16b-5"&gt;&lt;/a&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;p&gt;And in Scala:&lt;/p&gt;
&lt;pre class="code scala"&gt;&lt;a name="rest_code_87ca623eb8d64b039e0b3e803ddd02a5-1"&gt;&lt;/a&gt;&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BusPosition&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bus&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;coordinates&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timestamp&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;a name="rest_code_87ca623eb8d64b039e0b3e803ddd02a5-2"&gt;&lt;/a&gt;
&lt;a name="rest_code_87ca623eb8d64b039e0b3e803ddd02a5-3"&gt;&lt;/a&gt;&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;bus&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Bus&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"0.0,0.0"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"42"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"2019-01-01T12:23:22.813Z"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;a name="rest_code_87ca623eb8d64b039e0b3e803ddd02a5-4"&gt;&lt;/a&gt;
&lt;a name="rest_code_87ca623eb8d64b039e0b3e803ddd02a5-5"&gt;&lt;/a&gt;&lt;span class="n"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bus&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bus&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;a name="rest_code_87ca623eb8d64b039e0b3e803ddd02a5-6"&gt;&lt;/a&gt;&lt;span class="n"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bus&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;coordinates&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;p&gt;At first, it seems fine to use &lt;tt class="docutils literal"&gt;String&lt;/tt&gt; everywhere, but there's a bug in the above code... (The mistake is known as &lt;a class="reference external" href="https://dzone.com/articles/coping-with-stringly-typed-code"&gt;stringly-typed code&lt;/a&gt; and is not specific to either Scala or Java). Let's try again:&lt;/p&gt;
&lt;pre class="code scala"&gt;&lt;a name="rest_code_9a71d3fb08dd48bb90cf5601bec1c91d-1"&gt;&lt;/a&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.time.Instant&lt;/span&gt;
&lt;a name="rest_code_9a71d3fb08dd48bb90cf5601bec1c91d-2"&gt;&lt;/a&gt;
&lt;a name="rest_code_9a71d3fb08dd48bb90cf5601bec1c91d-3"&gt;&lt;/a&gt;&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TransportLine&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;AnyVal&lt;/span&gt;
&lt;a name="rest_code_9a71d3fb08dd48bb90cf5601bec1c91d-4"&gt;&lt;/a&gt;&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;VehicleId&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;AnyVal&lt;/span&gt;
&lt;a name="rest_code_9a71d3fb08dd48bb90cf5601bec1c91d-5"&gt;&lt;/a&gt;&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Coordinates&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;latitude&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;longtitude&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;a name="rest_code_9a71d3fb08dd48bb90cf5601bec1c91d-6"&gt;&lt;/a&gt;&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BusVehicle&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;VehicleId&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;TransportLine&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;initialDeparture&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Instant&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;a name="rest_code_9a71d3fb08dd48bb90cf5601bec1c91d-7"&gt;&lt;/a&gt;&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BusPosition&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bus&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;BusVehicle&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;coordinates&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Coordinates&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timestamp&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Instant&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;a name="rest_code_9a71d3fb08dd48bb90cf5601bec1c91d-8"&gt;&lt;/a&gt;
&lt;a name="rest_code_9a71d3fb08dd48bb90cf5601bec1c91d-9"&gt;&lt;/a&gt;&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;bus&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;BusPosition&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
&lt;a name="rest_code_9a71d3fb08dd48bb90cf5601bec1c91d-10"&gt;&lt;/a&gt;    &lt;span class="nc"&gt;BusVehicle&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;VehicleId&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"01fa3-a35"&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="nc"&gt;TransportLine&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"42"&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="nc"&gt;Instant&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;of&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2019&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="o"&gt;)),&lt;/span&gt;
&lt;a name="rest_code_9a71d3fb08dd48bb90cf5601bec1c91d-11"&gt;&lt;/a&gt;    &lt;span class="nc"&gt;Coordinates&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt;
&lt;a name="rest_code_9a71d3fb08dd48bb90cf5601bec1c91d-12"&gt;&lt;/a&gt;    &lt;span class="nc"&gt;Instant&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;of&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2019&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;22&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;813&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;a name="rest_code_9a71d3fb08dd48bb90cf5601bec1c91d-13"&gt;&lt;/a&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;p&gt;The above:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;is very concise (all definitions in one place)&lt;/li&gt;
&lt;li&gt;has a different type for every field in the &lt;cite&gt;BusPosition&lt;/cite&gt; class (which also makes the type definition easier to read without having to read the names)&lt;/li&gt;
&lt;li&gt;defines by virtue of being a case class: &lt;tt class="docutils literal"&gt;.equals&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;.hashCode&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;.toString&lt;/tt&gt; as well as &lt;tt class="docutils literal"&gt;.copy&lt;/tt&gt; which allows you to make a copy with only specific fields changed.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The &lt;tt class="docutils literal"&gt;extends AnyVal&lt;/tt&gt; is known as a &lt;a class="reference external" href="https://docs.scala-lang.org/overviews/core/value-classes.html"&gt;value class&lt;/a&gt; and allows to define a class with a single field that is not actually allocated at runtime. It allows us to clearly say what we mean when we write "42" for example - it is a transport line, but without any overhead except to declare the type and use it.&lt;/p&gt;
&lt;p&gt;How to represent this in Java? There are still at least &lt;a class="reference external" href="https://twitter.com/_JamesWard/status/1158756293877948418"&gt;two camps about&lt;/a&gt; how to define an equivalent in Java - either with a series of classes with getters and setters, or with immutable fields as above.&lt;/p&gt;
&lt;p&gt;With immutable fields, it would look like (split across several files):&lt;/p&gt;
&lt;pre class="code java"&gt;&lt;a name="rest_code_16f586b650a1435faa0da49e197a1751-1"&gt;&lt;/a&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.time.Instant&lt;/span&gt;
&lt;a name="rest_code_16f586b650a1435faa0da49e197a1751-2"&gt;&lt;/a&gt;
&lt;a name="rest_code_16f586b650a1435faa0da49e197a1751-3"&gt;&lt;/a&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TransportLine&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;a name="rest_code_16f586b650a1435faa0da49e197a1751-4"&gt;&lt;/a&gt;    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;a name="rest_code_16f586b650a1435faa0da49e197a1751-5"&gt;&lt;/a&gt;&lt;span class="o"&gt;}&lt;/span&gt;
&lt;a name="rest_code_16f586b650a1435faa0da49e197a1751-6"&gt;&lt;/a&gt;
&lt;a name="rest_code_16f586b650a1435faa0da49e197a1751-7"&gt;&lt;/a&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;VehicleId&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;a name="rest_code_16f586b650a1435faa0da49e197a1751-8"&gt;&lt;/a&gt;    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;a name="rest_code_16f586b650a1435faa0da49e197a1751-9"&gt;&lt;/a&gt;&lt;span class="o"&gt;}&lt;/span&gt;
&lt;a name="rest_code_16f586b650a1435faa0da49e197a1751-10"&gt;&lt;/a&gt;
&lt;a name="rest_code_16f586b650a1435faa0da49e197a1751-11"&gt;&lt;/a&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Coordinates&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;a name="rest_code_16f586b650a1435faa0da49e197a1751-12"&gt;&lt;/a&gt;    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;Double&lt;/span&gt; &lt;span class="n"&gt;latitude&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;a name="rest_code_16f586b650a1435faa0da49e197a1751-13"&gt;&lt;/a&gt;    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;Double&lt;/span&gt; &lt;span class="n"&gt;longitude&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;a name="rest_code_16f586b650a1435faa0da49e197a1751-14"&gt;&lt;/a&gt;&lt;span class="o"&gt;}&lt;/span&gt;
&lt;a name="rest_code_16f586b650a1435faa0da49e197a1751-15"&gt;&lt;/a&gt;
&lt;a name="rest_code_16f586b650a1435faa0da49e197a1751-16"&gt;&lt;/a&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BusVehicle&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;a name="rest_code_16f586b650a1435faa0da49e197a1751-17"&gt;&lt;/a&gt;    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;VehicleId&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;a name="rest_code_16f586b650a1435faa0da49e197a1751-18"&gt;&lt;/a&gt;    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;TransportLine&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;a name="rest_code_16f586b650a1435faa0da49e197a1751-19"&gt;&lt;/a&gt;    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;Instant&lt;/span&gt; &lt;span class="n"&gt;initialDeparture&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;a name="rest_code_16f586b650a1435faa0da49e197a1751-20"&gt;&lt;/a&gt;&lt;span class="o"&gt;}&lt;/span&gt;
&lt;a name="rest_code_16f586b650a1435faa0da49e197a1751-21"&gt;&lt;/a&gt;
&lt;a name="rest_code_16f586b650a1435faa0da49e197a1751-22"&gt;&lt;/a&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BusPosition&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;a name="rest_code_16f586b650a1435faa0da49e197a1751-23"&gt;&lt;/a&gt;    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;BusVehicle&lt;/span&gt; &lt;span class="n"&gt;bus&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;a name="rest_code_16f586b650a1435faa0da49e197a1751-24"&gt;&lt;/a&gt;    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;Coordinates&lt;/span&gt; &lt;span class="n"&gt;coordinates&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;a name="rest_code_16f586b650a1435faa0da49e197a1751-25"&gt;&lt;/a&gt;    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;Instant&lt;/span&gt; &lt;span class="n"&gt;timestamp&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;a name="rest_code_16f586b650a1435faa0da49e197a1751-26"&gt;&lt;/a&gt;&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;p&gt;This is far less concise than Scala, and we still haven't defined object equality (so you can't use this representation if you want to compare two &lt;tt class="docutils literal"&gt;BusPosition&lt;/tt&gt; in tests without defining &lt;tt class="docutils literal"&gt;equals&lt;/tt&gt; yourself or getting the IDE to generate it for you). If you want to use the getters/setters style, then you have to write even more code, and lose immutability as a result. (Immutability is not just important for reasoning about the code, but also for safe publication in the presence of concurrency). You can also use &lt;a class="reference external" href="https://projectlombok.org/features/Value"&gt;Project Lombok&lt;/a&gt;, which will instrument your code at runtime, but this doesn't come as built-in language feature.&lt;/p&gt;
&lt;p&gt;Scala field accesses are actually using compiler-generated getters for uniform field/getter access, which means that if you define:&lt;/p&gt;
&lt;pre class="code scala"&gt;&lt;a name="rest_code_2b99007b706d4f3aadd032d8536b7eee-1"&gt;&lt;/a&gt;&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Coordinates&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;latitude&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;longtitude&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;a name="rest_code_2b99007b706d4f3aadd032d8536b7eee-2"&gt;&lt;/a&gt;&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BusPositionLatLon&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lat&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lon&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;a name="rest_code_2b99007b706d4f3aadd032d8536b7eee-3"&gt;&lt;/a&gt;&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BusPositionCoordinates&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;coordinates&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Coordinates&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;a name="rest_code_2b99007b706d4f3aadd032d8536b7eee-4"&gt;&lt;/a&gt;    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;lat&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;coordinates&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;latitude&lt;/span&gt;
&lt;a name="rest_code_2b99007b706d4f3aadd032d8536b7eee-5"&gt;&lt;/a&gt;    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;lon&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;coordinates&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;longitude&lt;/span&gt;
&lt;a name="rest_code_2b99007b706d4f3aadd032d8536b7eee-6"&gt;&lt;/a&gt;&lt;span class="o"&gt;}&lt;/span&gt;
&lt;a name="rest_code_2b99007b706d4f3aadd032d8536b7eee-7"&gt;&lt;/a&gt;
&lt;a name="rest_code_2b99007b706d4f3aadd032d8536b7eee-8"&gt;&lt;/a&gt;&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;posA&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;BusPositionLatLon&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;1.2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;3.4&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;a name="rest_code_2b99007b706d4f3aadd032d8536b7eee-9"&gt;&lt;/a&gt;&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;posB&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;BusPositionCoordinates&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Coordinates&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;1.2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;3.4&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
&lt;a name="rest_code_2b99007b706d4f3aadd032d8536b7eee-10"&gt;&lt;/a&gt;
&lt;a name="rest_code_2b99007b706d4f3aadd032d8536b7eee-11"&gt;&lt;/a&gt;&lt;span class="n"&gt;posA&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lat&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;posB&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lat&lt;/span&gt;
&lt;a name="rest_code_2b99007b706d4f3aadd032d8536b7eee-12"&gt;&lt;/a&gt;&lt;span class="n"&gt;posA&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lon&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;posB&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lon&lt;/span&gt;
&lt;/pre&gt;&lt;p&gt;Then you have a uniform access syntax to both fields and methods defined without parens (the convention is to typically leave out the parentheses if the method is not performing any side effects).&lt;/p&gt;
&lt;p&gt;But this is just the basics (pattern matching is covered in the next section). Typically you have a situation where you want to model a case where you have a common type but different specific instances. Let's take the simplest (perhaps not the most illuminating example) of &lt;a class="reference external" href="https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html"&gt;Optional&lt;/a&gt; - where you have either:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;em&gt;something&lt;/em&gt; containing a value&lt;/li&gt;
&lt;li&gt;&lt;em&gt;nothing&lt;/em&gt; without any value&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In Java, this is modelled as a single class &lt;a class="reference external" href="https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html"&gt;Optional&lt;/a&gt;. In Scala, this is modelled with the abstract class &lt;a class="reference external" href="https://www.scala-lang.org/api/current/scala/Option.html"&gt;scala.Option&lt;/a&gt; which has a subclass &lt;tt class="docutils literal"&gt;Some&lt;/tt&gt; for the case with a value, and a subclass &lt;tt class="docutils literal"&gt;None&lt;/tt&gt; without. Actually the definition is a little more complex:&lt;/p&gt;
&lt;pre class="code scala"&gt;&lt;a name="rest_code_8609aa5e502b431f800b38daaafb6c77-1"&gt;&lt;/a&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;scala&lt;/span&gt;
&lt;a name="rest_code_8609aa5e502b431f800b38daaafb6c77-2"&gt;&lt;/a&gt;
&lt;a name="rest_code_8609aa5e502b431f800b38daaafb6c77-3"&gt;&lt;/a&gt;&lt;span class="k"&gt;sealed&lt;/span&gt; &lt;span class="k"&gt;abstract&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;+A&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="nc"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Product&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nc"&gt;Serializable&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;a name="rest_code_8609aa5e502b431f800b38daaafb6c77-4"&gt;&lt;/a&gt;
&lt;a name="rest_code_8609aa5e502b431f800b38daaafb6c77-5"&gt;&lt;/a&gt;&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Some&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;+A&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;a name="rest_code_8609aa5e502b431f800b38daaafb6c77-6"&gt;&lt;/a&gt;&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;None&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Nothing&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/pre&gt;&lt;p&gt;In the above:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;cite&gt;+&lt;/cite&gt; is a &lt;a class="reference external" href="https://docs.scala-lang.org/tour/variances.html"&gt;variance&lt;/a&gt; annotation that implies &lt;cite&gt;Option&lt;/cite&gt; is covariant&lt;/li&gt;
&lt;li&gt;&lt;cite&gt;sealed&lt;/cite&gt; keyword tells the compiler that all the subclasses of &lt;cite&gt;Option&lt;/cite&gt; are known at compile-time&lt;/li&gt;
&lt;li&gt;&lt;cite&gt;case object None&lt;/cite&gt; defines a singleton object that is an &lt;tt class="docutils literal"&gt;Option[Nothing]&lt;/tt&gt;. This can be a singleton because &lt;tt class="docutils literal"&gt;Option&lt;/tt&gt; is covariant, and &lt;tt class="docutils literal"&gt;Nothing&lt;/tt&gt; is the bottom type (a subtype of every other type)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Why is the definition like it is and why is it better than Java's ``Optional``?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;We now have two concrete types: &lt;cite&gt;Some&lt;/cite&gt; and &lt;cite&gt;None&lt;/cite&gt;, and there are no other subtypes of &lt;cite&gt;Option&lt;/cite&gt;. This means &lt;cite&gt;Option&lt;/cite&gt; meets the definition of a &lt;a class="reference external" href="https://en.wikipedia.org/wiki/Algebraic_data_type"&gt;Algebraic Data Type&lt;/a&gt; (or ADT for short), specifically a &lt;em&gt;coproduct&lt;/em&gt;. The fact that &lt;cite&gt;Option&lt;/cite&gt; is sealed allows us to do pattern matching exhaustively. This gives enormous type safety that simply isn't available in Java with &lt;cite&gt;instanceof&lt;/cite&gt;. With ADTs we also have a uniform way of treating &lt;em&gt;any&lt;/em&gt; type that is defined with &lt;cite&gt;sealed&lt;/cite&gt; uniformly, which is a win for being able to reason about code.&lt;/p&gt;
&lt;p&gt;With &lt;a class="reference external" href="https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html"&gt;Optional&lt;/a&gt;, we cannot determine whether a value exists without calling the &lt;cite&gt;.isDefined&lt;/cite&gt; or &lt;cite&gt;.isEmpty&lt;/cite&gt; method, and certainly not by examining the types. But the example above is just one example - there are many instances where we would like to have compile-time certainty about handling all the cases of something. It is possible to use ADTs in Java with libraries like &lt;a class="reference external" href="https://github.com/spotify/dataenum"&gt;dataenum&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="pattern-matching"&gt;
&lt;h3&gt;&lt;a class="toc-backref" href="https://blog.solidninja.is/posts/why-scala/#id9"&gt;Pattern matching&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Perhaps the case for case classes wouldn't be so strong if Scala was lacking another critical feature: &lt;a class="reference external" href="https://docs.scala-lang.org/tour/pattern-matching.html"&gt;pattern matching&lt;/a&gt;. Pattern matching is not just a safer version of &lt;tt class="docutils literal"&gt;instanceof&lt;/tt&gt; in Java, it also allows to match on fields inside case classes, to do name binding, on values or to write guards for specific conditions. From the excellent &lt;a class="reference external" href="https://danielwestheide.com/scala/neophytes.html"&gt;Neophyte's Guide to Scala&lt;/a&gt;, &lt;a class="reference external" href="https://danielwestheide.com/blog/2012/12/05/the-neophytes-guide-to-scala-part-3-patterns-everywhere.html"&gt;patterns everywhere&lt;/a&gt; chapter:&lt;/p&gt;
&lt;pre class="code scala"&gt;&lt;a name="rest_code_aa4ebbc7404d4075a7ef30e23b7f2e4e-1"&gt;&lt;/a&gt;&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Player&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;a name="rest_code_aa4ebbc7404d4075a7ef30e23b7f2e4e-2"&gt;&lt;/a&gt;
&lt;a name="rest_code_aa4ebbc7404d4075a7ef30e23b7f2e4e-3"&gt;&lt;/a&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;printMessage&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;player&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Player&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;player&lt;/span&gt; &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;a name="rest_code_aa4ebbc7404d4075a7ef30e23b7f2e4e-4"&gt;&lt;/a&gt;  &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Player&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Try harder ;)"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;a name="rest_code_aa4ebbc7404d4075a7ef30e23b7f2e4e-5"&gt;&lt;/a&gt;  &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Player&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;100000&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Get a job, dude!"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;a name="rest_code_aa4ebbc7404d4075a7ef30e23b7f2e4e-6"&gt;&lt;/a&gt;  &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Player&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;s"Hey &lt;/span&gt;&lt;span class="si"&gt;$name&lt;/span&gt;&lt;span class="s"&gt;, nice to see you again!"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;a name="rest_code_aa4ebbc7404d4075a7ef30e23b7f2e4e-7"&gt;&lt;/a&gt;&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;p&gt;In the above example:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;destructuring the case class &lt;tt class="docutils literal"&gt;Player&lt;/tt&gt; by putting name bindings to fields of &lt;tt class="docutils literal"&gt;Player&lt;/tt&gt;, and &lt;tt class="docutils literal"&gt;_&lt;/tt&gt; underscore otherwise&lt;/li&gt;
&lt;li&gt;used literal value &lt;tt class="docutils literal"&gt;0&lt;/tt&gt; to distinguish the case where the score was zero&lt;/li&gt;
&lt;li&gt;used a pattern guard to check for high scores&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This could have been written in Java as follows:&lt;/p&gt;
&lt;pre class="code java"&gt;&lt;a name="rest_code_2329e49c2ebe4a2fa1ca7db6c7edfb4a-1"&gt;&lt;/a&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;printMessage&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Player&lt;/span&gt; &lt;span class="n"&gt;player&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;a name="rest_code_2329e49c2ebe4a2fa1ca7db6c7edfb4a-2"&gt;&lt;/a&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;player&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;score&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;a name="rest_code_2329e49c2ebe4a2fa1ca7db6c7edfb4a-3"&gt;&lt;/a&gt;        &lt;span class="n"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Try harder ;)"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;a name="rest_code_2329e49c2ebe4a2fa1ca7db6c7edfb4a-4"&gt;&lt;/a&gt;    &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;player&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;score&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;a name="rest_code_2329e49c2ebe4a2fa1ca7db6c7edfb4a-5"&gt;&lt;/a&gt;        &lt;span class="n"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Get a job, dude!"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;a name="rest_code_2329e49c2ebe4a2fa1ca7db6c7edfb4a-6"&gt;&lt;/a&gt;    &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;a name="rest_code_2329e49c2ebe4a2fa1ca7db6c7edfb4a-7"&gt;&lt;/a&gt;        &lt;span class="n"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hey "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;player&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;", nice to see you again!"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;a name="rest_code_2329e49c2ebe4a2fa1ca7db6c7edfb4a-8"&gt;&lt;/a&gt;    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;a name="rest_code_2329e49c2ebe4a2fa1ca7db6c7edfb4a-9"&gt;&lt;/a&gt;&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;p&gt;And given the above was a pretty simple example, the code looks very similar.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;But what about the case when ADTs are involved?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;For example, if we want to model the bus location problem again, but now we have other types of transport:&lt;/p&gt;
&lt;pre class="code scala"&gt;&lt;a name="rest_code_7e8bbc0f93ce46fb85637057b329b361-1"&gt;&lt;/a&gt;&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="k"&gt;sealed&lt;/span&gt; &lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="nc"&gt;TransportVehicle&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;a name="rest_code_7e8bbc0f93ce46fb85637057b329b361-2"&gt;&lt;/a&gt;    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Coordinates&lt;/span&gt;
&lt;a name="rest_code_7e8bbc0f93ce46fb85637057b329b361-3"&gt;&lt;/a&gt;&lt;span class="o"&gt;}&lt;/span&gt;
&lt;a name="rest_code_7e8bbc0f93ce46fb85637057b329b361-4"&gt;&lt;/a&gt;
&lt;a name="rest_code_7e8bbc0f93ce46fb85637057b329b361-5"&gt;&lt;/a&gt;&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Bus&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Coordinates&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sittingCapacity&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;standingCapacity&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;TransportVehicle&lt;/span&gt;
&lt;a name="rest_code_7e8bbc0f93ce46fb85637057b329b361-6"&gt;&lt;/a&gt;&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;licensePlate&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Coordinates&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;numSeats&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;TransportVehicle&lt;/span&gt;
&lt;a name="rest_code_7e8bbc0f93ce46fb85637057b329b361-7"&gt;&lt;/a&gt;&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RidesharingVehicle&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;company&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Long&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Coordinates&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;capacity&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;TransportVehicle&lt;/span&gt;
&lt;a name="rest_code_7e8bbc0f93ce46fb85637057b329b361-8"&gt;&lt;/a&gt;&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Tram&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt;  &lt;span class="kt"&gt;Coordinates&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sittingCapacity&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;standingCapacity&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;TransportVehicle&lt;/span&gt;
&lt;a name="rest_code_7e8bbc0f93ce46fb85637057b329b361-9"&gt;&lt;/a&gt;
&lt;a name="rest_code_7e8bbc0f93ce46fb85637057b329b361-10"&gt;&lt;/a&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;findVehicleForThreePeople&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vehicles&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;TransportVehicle&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;TransportVehicle&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;vehicles&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;find&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;a name="rest_code_7e8bbc0f93ce46fb85637057b329b361-11"&gt;&lt;/a&gt;    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;bus&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bus&lt;/span&gt;               &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;bus&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sittingCapacity&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;bus&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;standingCapacity&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;a name="rest_code_7e8bbc0f93ce46fb85637057b329b361-12"&gt;&lt;/a&gt;    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;car&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Car&lt;/span&gt;               &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;car&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;numSeats&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;a name="rest_code_7e8bbc0f93ce46fb85637057b329b361-13"&gt;&lt;/a&gt;    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;rs&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;RidesharingVehicle&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;rs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;capacity&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;a name="rest_code_7e8bbc0f93ce46fb85637057b329b361-14"&gt;&lt;/a&gt;    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;tram&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Tram&lt;/span&gt;             &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;tram&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sittingCapacity&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;tram&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;standingCapacity&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;a name="rest_code_7e8bbc0f93ce46fb85637057b329b361-15"&gt;&lt;/a&gt;&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;p&gt;The above function is still a little simplistic (hard-coded constraints, no interface for capacity calculation) but it is still:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;Clear and concise - the business logic (seats &amp;gt;= 3) is encoded in one place without any unnecessary detail&lt;/li&gt;
&lt;li&gt;Exhaustive - if you add a new vehicle type, the compiler will warn that you have a new case to handle&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In Java, this would look like:&lt;/p&gt;
&lt;pre class="code java"&gt;&lt;a name="rest_code_73232083597a47b6b3daefde84de95a0-1"&gt;&lt;/a&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;TransportVehicle&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;findVehicleForThreePeople&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;TransportVehicle&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;vehicles&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;a name="rest_code_73232083597a47b6b3daefde84de95a0-2"&gt;&lt;/a&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;vehicles&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;filter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vehicle&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;a name="rest_code_73232083597a47b6b3daefde84de95a0-3"&gt;&lt;/a&gt;        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vehicle&lt;/span&gt; &lt;span class="n"&gt;isinstance&lt;/span&gt; &lt;span class="n"&gt;Bus&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;a name="rest_code_73232083597a47b6b3daefde84de95a0-4"&gt;&lt;/a&gt;            &lt;span class="n"&gt;Bus&lt;/span&gt; &lt;span class="n"&gt;bus&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Bus&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="n"&gt;vehicle&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;a name="rest_code_73232083597a47b6b3daefde84de95a0-5"&gt;&lt;/a&gt;            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;bus&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sittingCapacity&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;bus&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;standingCapacity&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;a name="rest_code_73232083597a47b6b3daefde84de95a0-6"&gt;&lt;/a&gt;        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vehicle&lt;/span&gt; &lt;span class="n"&gt;isinstance&lt;/span&gt; &lt;span class="n"&gt;Car&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;a name="rest_code_73232083597a47b6b3daefde84de95a0-7"&gt;&lt;/a&gt;            &lt;span class="n"&gt;Car&lt;/span&gt; &lt;span class="n"&gt;car&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Car&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="n"&gt;vehicle&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;a name="rest_code_73232083597a47b6b3daefde84de95a0-8"&gt;&lt;/a&gt;            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;car&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;numSeats&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;a name="rest_code_73232083597a47b6b3daefde84de95a0-9"&gt;&lt;/a&gt;        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vehicle&lt;/span&gt; &lt;span class="n"&gt;isinstance&lt;/span&gt; &lt;span class="n"&gt;Tram&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;a name="rest_code_73232083597a47b6b3daefde84de95a0-10"&gt;&lt;/a&gt;            &lt;span class="n"&gt;Tram&lt;/span&gt; &lt;span class="n"&gt;tram&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Tram&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="n"&gt;vehicle&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;a name="rest_code_73232083597a47b6b3daefde84de95a0-11"&gt;&lt;/a&gt;            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;tram&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sittingCapacity&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;tram&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;standingCapacity&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;a name="rest_code_73232083597a47b6b3daefde84de95a0-12"&gt;&lt;/a&gt;        &lt;span class="o"&gt;}&lt;/span&gt;
&lt;a name="rest_code_73232083597a47b6b3daefde84de95a0-13"&gt;&lt;/a&gt;    &lt;span class="o"&gt;}).&lt;/span&gt;&lt;span class="na"&gt;findAny&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;a name="rest_code_73232083597a47b6b3daefde84de95a0-14"&gt;&lt;/a&gt;&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;p&gt;Did you spot the missing case above? The compiler didn't. At least there is a &lt;a class="reference external" href="https://cr.openjdk.java.net/~briangoetz/amber/pattern-match.html"&gt;proposal for pattern matching&lt;/a&gt; in Java now.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="implicits-given"&gt;
&lt;h3&gt;&lt;a class="toc-backref" href="https://blog.solidninja.is/posts/why-scala/#id10"&gt;Implicits/given&lt;/a&gt;&lt;/h3&gt;
&lt;div class="admonition note"&gt;
&lt;p class="first admonition-title"&gt;Note&lt;/p&gt;
&lt;p class="last"&gt;&lt;a class="reference external" href="http://dotty.epfl.ch/docs/reference/contextual/relationship-implicits.html"&gt;given&lt;/a&gt; is a new mechanism coming in Scala 3 that improve upon the shortcomings of implicits in Scala 2.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a class="reference external" href="https://docs.scala-lang.org/tour/implicit-parameters.html"&gt;Implicits&lt;/a&gt; are I think &lt;em&gt;the&lt;/em&gt; feature in Scala that polarizes opinions the most. You either love it for the boilerplate reduction or hate it because of the perceived complexity and arcane rules of resolution (or because someone in your old project went wild with implicit conversions and made a mess).&lt;/p&gt;
&lt;p&gt;There are 3 uses of &lt;tt class="docutils literal"&gt;implicit&lt;/tt&gt; in Scala 2 at the moment:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;Implicit &lt;em&gt;classes&lt;/em&gt;, which allow you to declare extension methods on classes that you either didn't define yourself or are not able to change for backwards compatibility reasons&lt;/li&gt;
&lt;li&gt;Implicit &lt;em&gt;conversions&lt;/em&gt;, which allow the compiler to auto-magically convert a value from one type to another&lt;/li&gt;
&lt;li&gt;Implicit &lt;em&gt;parameters&lt;/em&gt;, which allow the compiler to inject an instance of a given class at compile-time, based purely on the imports and the implicit scoping rules&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The details have been widely covered: &lt;a class="reference external" href="http://www.lihaoyi.com/post/ImplicitDesignPatternsinScala.html"&gt;Implicit Design Patterns in Scala&lt;/a&gt; or &lt;a class="reference external" href="https://kubuszok.com/2018/implicits-type-classes-and-extension-methods-part-1/"&gt;Implicits, type classes, and extension methods&lt;/a&gt; to give just two good posts.&lt;/p&gt;
&lt;p&gt;For me, implicits are a great way to change run-time problems into compile-time problems (eliminating errors earlier) - especially when doing dependency injection or encoder/decoder definition.&lt;/p&gt;
&lt;p&gt;Let's the take serialization example for JSON. In Java, you typically have a runtime mechanism for that using a library like &lt;a class="reference external" href="https://github.com/FasterXML/jackson"&gt;Jackson&lt;/a&gt; that is reflection-based. If you look at some common &lt;a class="reference external" href="https://www.baeldung.com/jackson-exception"&gt;Jackson exceptions&lt;/a&gt;, you can see things like:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;JsonMappingException: Can not construct instance of&lt;/li&gt;
&lt;li&gt;JsonMappingException: No suitable constructor&lt;/li&gt;
&lt;li&gt;JsonMappingException: No serializer found for class&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These can disappear if instead you use a library like &lt;a class="reference external" href="https://github.com/circe/circe"&gt;circe&lt;/a&gt; which can do the work at compile-time:&lt;/p&gt;
&lt;pre class="code scala"&gt;&lt;a name="rest_code_57aea89cc3fc4fba992e5824e10dbd66-1"&gt;&lt;/a&gt;&lt;span class="c1"&gt;// model definition&lt;/span&gt;
&lt;a name="rest_code_57aea89cc3fc4fba992e5824e10dbd66-2"&gt;&lt;/a&gt;&lt;span class="k"&gt;sealed&lt;/span&gt; &lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="nc"&gt;SiteUser&lt;/span&gt;
&lt;a name="rest_code_57aea89cc3fc4fba992e5824e10dbd66-3"&gt;&lt;/a&gt;&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Anonymous&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;SiteUser&lt;/span&gt;
&lt;a name="rest_code_57aea89cc3fc4fba992e5824e10dbd66-4"&gt;&lt;/a&gt;&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LoggedInUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;SiteUser&lt;/span&gt;
&lt;a name="rest_code_57aea89cc3fc4fba992e5824e10dbd66-5"&gt;&lt;/a&gt;
&lt;a name="rest_code_57aea89cc3fc4fba992e5824e10dbd66-6"&gt;&lt;/a&gt;&lt;span class="c1"&gt;// json protocol definition&lt;/span&gt;
&lt;a name="rest_code_57aea89cc3fc4fba992e5824e10dbd66-7"&gt;&lt;/a&gt;
&lt;a name="rest_code_57aea89cc3fc4fba992e5824e10dbd66-8"&gt;&lt;/a&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;io.circe.generic.extras.Configuration&lt;/span&gt;
&lt;a name="rest_code_57aea89cc3fc4fba992e5824e10dbd66-9"&gt;&lt;/a&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;io.circe.generic.extras.semiauto._&lt;/span&gt;
&lt;a name="rest_code_57aea89cc3fc4fba992e5824e10dbd66-10"&gt;&lt;/a&gt;
&lt;a name="rest_code_57aea89cc3fc4fba992e5824e10dbd66-11"&gt;&lt;/a&gt;&lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;configuration&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Configuration&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Configuration&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;default&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;withSnakeCaseMemberNames&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;withDiscriminator&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"type"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;a name="rest_code_57aea89cc3fc4fba992e5824e10dbd66-12"&gt;&lt;/a&gt;
&lt;a name="rest_code_57aea89cc3fc4fba992e5824e10dbd66-13"&gt;&lt;/a&gt;&lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;siteUserEncoder&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Encoder&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;SiteUser&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;deriveEncoder&lt;/span&gt;
&lt;a name="rest_code_57aea89cc3fc4fba992e5824e10dbd66-14"&gt;&lt;/a&gt;
&lt;a name="rest_code_57aea89cc3fc4fba992e5824e10dbd66-15"&gt;&lt;/a&gt;&lt;span class="c1"&gt;// usage&lt;/span&gt;
&lt;a name="rest_code_57aea89cc3fc4fba992e5824e10dbd66-16"&gt;&lt;/a&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;io.circe.syntax._&lt;/span&gt;
&lt;a name="rest_code_57aea89cc3fc4fba992e5824e10dbd66-17"&gt;&lt;/a&gt;
&lt;a name="rest_code_57aea89cc3fc4fba992e5824e10dbd66-18"&gt;&lt;/a&gt;&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;anon&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;SiteUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Anonymous&lt;/span&gt;
&lt;a name="rest_code_57aea89cc3fc4fba992e5824e10dbd66-19"&gt;&lt;/a&gt;&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;loggedIn&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;SiteUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;LoggedInUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"bob"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;a name="rest_code_57aea89cc3fc4fba992e5824e10dbd66-20"&gt;&lt;/a&gt;
&lt;a name="rest_code_57aea89cc3fc4fba992e5824e10dbd66-21"&gt;&lt;/a&gt;&lt;span class="n"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;anon&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;asJson&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;a name="rest_code_57aea89cc3fc4fba992e5824e10dbd66-22"&gt;&lt;/a&gt;&lt;span class="n"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;loggedIn&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;asJson&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;p&gt;The above will print:&lt;/p&gt;
&lt;pre class="code json"&gt;&lt;a name="rest_code_3cbcbbbdb7f64940be704b22a188e5f2-1"&gt;&lt;/a&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;a name="rest_code_3cbcbbbdb7f64940be704b22a188e5f2-2"&gt;&lt;/a&gt;  &lt;span class="nt"&gt;"type"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Anonymous"&lt;/span&gt;
&lt;a name="rest_code_3cbcbbbdb7f64940be704b22a188e5f2-3"&gt;&lt;/a&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;a name="rest_code_3cbcbbbdb7f64940be704b22a188e5f2-4"&gt;&lt;/a&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;a name="rest_code_3cbcbbbdb7f64940be704b22a188e5f2-5"&gt;&lt;/a&gt;  &lt;span class="nt"&gt;"id"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;a name="rest_code_3cbcbbbdb7f64940be704b22a188e5f2-6"&gt;&lt;/a&gt;  &lt;span class="nt"&gt;"name"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"bob"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;a name="rest_code_3cbcbbbdb7f64940be704b22a188e5f2-7"&gt;&lt;/a&gt;  &lt;span class="nt"&gt;"type"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"LoggedInUser"&lt;/span&gt;
&lt;a name="rest_code_3cbcbbbdb7f64940be704b22a188e5f2-8"&gt;&lt;/a&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;p&gt;If you add more cases to &lt;cite&gt;SiteUser&lt;/cite&gt; or more fields, the derived encoder will keep compiling unless the fields have no encoder already defined for them. The resolution is done at compile-time because &lt;tt class="docutils literal"&gt;implicit&lt;/tt&gt; is the mechanism under the hood. This guarantees that you will never get a runtime error unless the JSON itself is invalid (which is handled by all the fallible conversions returning an &lt;tt class="docutils literal"&gt;Either[Error, A]&lt;/tt&gt; ADT).&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="macros"&gt;
&lt;h3&gt;&lt;a class="toc-backref" href="https://blog.solidninja.is/posts/why-scala/#id11"&gt;Macros&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Another advanced mechanism that Scala offers for generating code at compile-time is &lt;em&gt;macros&lt;/em&gt; (&lt;a class="reference external" href="https://scalameta.org/"&gt;scalameta&lt;/a&gt;). You typically won't use the feature directly unless you're a library author, so why is it relevant here?&lt;/p&gt;
&lt;p&gt;Anecdote: at one of the projects recently I had a case where the JSON structure of an upstream system and the BigQuery database structure was identical (and was guaranteed to stay identical). To avoid duplicating code (and writing lots of error-prone manual mapping), I decided to reuse the case classes from the JSON world in the database mapping. BigQuery naturally only returned &lt;tt class="docutils literal"&gt;Map[String, Any]&lt;/tt&gt; so I had to find a way to automatically populate the relevant case class. This was a perfect case for &lt;em&gt;generic derivation&lt;/em&gt; (see &lt;a class="reference external" href="https://meta.plasm.us/posts/2015/11/08/type-classes-and-generic-derivation/"&gt;type classes and generic derivation&lt;/a&gt; for a more complete example).&lt;/p&gt;
&lt;p&gt;I wanted to have a trait (typeclass) that looked like:&lt;/p&gt;
&lt;pre class="code scala"&gt;&lt;a name="rest_code_017930dcfe1b4d279e8282bd656bcf4c-1"&gt;&lt;/a&gt;&lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="nc"&gt;Decoder&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;a name="rest_code_017930dcfe1b4d279e8282bd656bcf4c-2"&gt;&lt;/a&gt;    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;decode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bigQueryResult&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;BQ&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Either&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Error&lt;/span&gt;, &lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;a name="rest_code_017930dcfe1b4d279e8282bd656bcf4c-3"&gt;&lt;/a&gt;&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;p&gt;But not have to write it manually for each case class I wanted to query. Luckily, the &lt;a class="reference external" href="https://propensive.com/opensource/magnolia/"&gt;magnolia&lt;/a&gt; library made it fairly straightforward to create a derivation for that, and I had a working example I could use within two days (for a fairly complex data type with lots of nested lists, maps, etc.). The initial time investment probably was similar to writing the manual mapping by hand, but since the investment is generic over any case class type, no mapping would need to be done manually in the future, saving time and reducing errors.&lt;/p&gt;
&lt;p&gt;There is no direct equivalent of a macro library for Java - there is &lt;a class="reference external" href="https://asm.ow2.io/"&gt;asm&lt;/a&gt; which does bytecode manipulation, but that is a lot lower level and &lt;a class="reference external" href="https://propensive.com/opensource/magnolia/"&gt;magnolia&lt;/a&gt; supports coproducts and case classes which do not exist as high-level concepts in Java.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="summary"&gt;
&lt;h2&gt;&lt;a class="toc-backref" href="https://blog.solidninja.is/posts/why-scala/#id12"&gt;Summary&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Scala for me offers both simple and advanced mechanisms to manage complexity (via ADTs, case classes and pattern matching), eliminate run-time errors by pushing checks to compile-time and conciseness. The reduction in code is significant because it both reduces bugs (no bugs in code that isn't written) and allows you to get up-to-speed with a new codebase quicker (there is less to read). The functional style of programming and the &lt;a class="reference external" href="https://typelevel.org/"&gt;typelevel&lt;/a&gt; ecosystem of libraries make constructing software safer (at compile-time even before tests are involved) and more productive (due to the high level of genericity and low level of boilerplate). Functional style code typically allows for better &lt;em&gt;local reasoning&lt;/em&gt; which is easier on the brains of developers and code reviewers.&lt;/p&gt;
&lt;p&gt;I also ommitted a lot of smaller feature that Scala has such as for comprehensions, companion objects, type aliases, as well as patterns when writing scala code like typeclasses.&lt;/p&gt;
&lt;p&gt;One disadvantage is that it will not take one day or even a week to get to know Scala well. I also feel that prototyping a new service when requirements are changing quickly is also quite difficult &lt;em&gt;due to&lt;/em&gt; the static typing - sometimes it is easier to let everything be a string or a map, in which case a more dynamic language like Python or Clojure may be quicker to use initially.&lt;/p&gt;
&lt;p&gt;Please leave comments or take a look at my &lt;a class="reference external" href="https://github.com/solidninja/example-pure-todomvc"&gt;example-pure-todomvc&lt;/a&gt; for more concrete example of what a Scala microservice that implements TodoMVC looks like.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;</description><category>functional programming</category><category>scala</category><guid>https://blog.solidninja.is/posts/why-scala/</guid><pubDate>Mon, 05 Aug 2019 10:00:00 GMT</pubDate></item><item><title>scala.world conference notes</title><link>https://blog.solidninja.is/posts/scala-world-2016/</link><dc:creator>Vladimir Lushnikov</dc:creator><description>&lt;div&gt;&lt;div class="figure align-center"&gt;
&lt;a class="reference external image-reference" href="https://blog.solidninja.is/galleries/scala-world-2016/22_scala-world-2016.jpg"&gt;&lt;img alt="#scalahike" src="https://blog.solidninja.is/galleries/scala-world-2016/22_scala-world-2016.thumbnail.jpg"&gt;&lt;/a&gt;
&lt;p class="caption"&gt;Scala Hikers @ &lt;a class="reference external" href="https://en.wikipedia.org/wiki/Scafell_Pike"&gt;Scarfell Pike&lt;/a&gt; - &lt;a class="reference external" href="https://blog.solidninja.is/galleries/scala-world-2016/"&gt;see here for more&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;I attended the &lt;a class="reference external" href="https://scala.world"&gt;scala.world&lt;/a&gt; conference that took place in the Lake District between the 12th and 13th September 2016. These are some notes on the conference, mainly for the benefit of my colleagues but also as a convenient way to start off the SOLID Ninja blog.&lt;/p&gt;
&lt;div class="section" id="the-hiking"&gt;
&lt;h2&gt;The hiking&lt;/h2&gt;
&lt;p&gt;Notes on scala.world would not be complete without mentioning the hiking that took place over the preceding weekend. The Lake District weather gods were favourable throughout the conference which was unusual to say the least (at least if you live in England). On the Saturday there was an organized hike to &lt;a class="reference external" href="https://en.wikipedia.org/wiki/Scafell_Pike"&gt;Scarfell Pike&lt;/a&gt; which is the tallest point in England and on Sunday there was a choice of cycling and hiking again. Needless to say even the "easy" route does not look like a bike ride through the Netherlands. &lt;a class="reference external" href="https://blog.solidninja.is/galleries/scala-world-2016/"&gt;Photos are here.&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The hiking was a great way to meet people and talk about life rather than &lt;a class="reference external" href="https://issues.scala-lang.org/browse/SI-2712"&gt;SI-2712&lt;/a&gt;...&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="the-conference"&gt;
&lt;h2&gt;The conference&lt;/h2&gt;
&lt;p&gt;The schedule was finally up on Monday morning about 4am - so the conference could begin!&lt;/p&gt;
&lt;p&gt;In no particular order, these are the talks/workshops I made notes on:&lt;/p&gt;
&lt;div class="section" id="martin-s-keynote"&gt;
&lt;h3&gt;Martin's Keynote&lt;/h3&gt;
&lt;p&gt;Martin Odersky was up first with a keynote titled &lt;cite&gt;Compilers are (in memory) databases&lt;/cite&gt; (which admittedly has been given before so check below for the YouTube video). It is very nice to see that &lt;a class="reference external" href="https://github.com/lampepfl/dotty/blob/master/src/dotty/tools/dotc/Compiler.scala"&gt;dotc&lt;/a&gt; has got lots of small, reasonable passes in the compiler rather than a few big monolithic passes.&lt;/p&gt;
&lt;div class="youtube-video"&gt;
&lt;iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/WxyyJyB_Ssc?rel=0&amp;amp;wmode=transparent" frameborder="0" allow="encrypted-media" allowfullscreen&gt;&lt;/iframe&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;div class="section" id="cbt"&gt;
&lt;h3&gt;CBT&lt;/h3&gt;
&lt;p&gt;&lt;a class="reference external" href="https://github.com/cvogt/cbt"&gt;CBT&lt;/a&gt; is in my opinion the &lt;strong&gt;S&lt;/strong&gt;(imple)&lt;strong&gt;B&lt;/strong&gt;(uild)&lt;strong&gt;T&lt;/strong&gt;(ool) that should have been - Christian did a great talk explaining how cbt emerged from the design of &lt;a class="reference external" href="http://www.scala-sbt.org/"&gt;SBT&lt;/a&gt;. The slides are not yet up - here is the best I could do:&lt;/p&gt;

&lt;div id="slides_2258d9f0dce74fc59d7ec8ec4b241dfa" class="carousel slide" data-ride="carousel"&gt;
    &lt;ol class="carousel-indicators"&gt;
            &lt;li data-target="#slides_2258d9f0dce74fc59d7ec8ec4b241dfa" data-slide-to="0" class="active"&gt;
            &lt;/li&gt;&lt;li data-target="#slides_2258d9f0dce74fc59d7ec8ec4b241dfa" data-slide-to="1"&gt;
            &lt;/li&gt;&lt;li data-target="#slides_2258d9f0dce74fc59d7ec8ec4b241dfa" data-slide-to="2"&gt;
    &lt;/li&gt;&lt;/ol&gt;
    &lt;div class="carousel-inner"&gt;
                &lt;div class="carousel-item active"&gt;&lt;img class="d-block w-100" src="https://blog.solidninja.is/galleries/scala-world-2016/61_scala-world-2016.jpg" alt=""&gt;&lt;/div&gt;
                &lt;div class="carousel-item"&gt;&lt;img class="d-block w-100" src="https://blog.solidninja.is/galleries/scala-world-2016/62_scala-world-2016.jpg" alt=""&gt;&lt;/div&gt;
                &lt;div class="carousel-item"&gt;&lt;img class="d-block w-100" src="https://blog.solidninja.is/galleries/scala-world-2016/63_scala-world-2016.jpg" alt=""&gt;&lt;/div&gt;
    &lt;/div&gt;
      &lt;a class="carousel-control-prev" href="https://blog.solidninja.is/posts/scala-world-2016/#slides_2258d9f0dce74fc59d7ec8ec4b241dfa" role="button" data-slide="prev"&gt;
    &lt;span class="carousel-control-prev-icon" aria-hidden="true"&gt;&lt;/span&gt;
    &lt;span class="sr-only"&gt;Previous&lt;/span&gt;
  &lt;/a&gt;
  &lt;a class="carousel-control-next" href="https://blog.solidninja.is/posts/scala-world-2016/#slides_2258d9f0dce74fc59d7ec8ec4b241dfa" role="button" data-slide="next"&gt;
    &lt;span class="carousel-control-next-icon" aria-hidden="true"&gt;&lt;/span&gt;
    &lt;span class="sr-only"&gt;Next&lt;/span&gt;
  &lt;/a&gt;
&lt;/div&gt;

&lt;p&gt;CBT looks very promising because:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;The model is much simpler than SBT so you do not need to know about &lt;a class="reference external" href="http://www.scala-sbt.org/release/docs/Scopes.html"&gt;scope axes&lt;/a&gt;&lt;ul&gt;
&lt;li&gt;I doubt anyone will be writing a &lt;a class="reference external" href="https://www.manning.com/books/sbt-in-action"&gt;book&lt;/a&gt; about cbt though...&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;It's fast! (both to startup and resolve dependencies)&lt;ul&gt;
&lt;li&gt;No ivy involvement helps a lot in this respect, though support for &lt;a class="reference external" href="https://github.com/alexarchambault/coursier"&gt;coursier&lt;/a&gt; is currently not in a good shape&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;There is no DSL - everything is a project and tasks are functions, so you can write your own plugins etc. the same way you always write code (in Scala)&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="generic-derivation"&gt;
&lt;h3&gt;Generic derivation&lt;/h3&gt;
&lt;p&gt;I did not go to this one, but Travis Brown did an interesting talk on &lt;a class="reference external" href="https://meta.plasm.us/slides/scalaworld/#1"&gt;Generic derivation&lt;/a&gt; (in &lt;a class="reference external" href="https://github.com/travisbrown/circe-derivation"&gt;Circe&lt;/a&gt;, which is a &lt;a class="reference external" href="https://github.com/travisbrown/circe"&gt;fast json library&lt;/a&gt;)&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="the-type-astronaut-s-guide-to-shapeless"&gt;
&lt;h3&gt;The Type Astronaut's Guide to Shapeless&lt;/h3&gt;
&lt;p&gt;With a title like that it was going to be hard to disappoint, and Dave Gurnell's talk about &lt;a class="reference external" href="https://github.com/milessabin/shapeless"&gt;shapeless&lt;/a&gt; was very good:&lt;/p&gt;

&lt;div id="slides_b68a62326bb04179ba859673c3691d90" class="carousel slide" data-ride="carousel"&gt;
    &lt;ol class="carousel-indicators"&gt;
            &lt;li data-target="#slides_b68a62326bb04179ba859673c3691d90" data-slide-to="0" class="active"&gt;
            &lt;/li&gt;&lt;li data-target="#slides_b68a62326bb04179ba859673c3691d90" data-slide-to="1"&gt;
            &lt;/li&gt;&lt;li data-target="#slides_b68a62326bb04179ba859673c3691d90" data-slide-to="2"&gt;
            &lt;/li&gt;&lt;li data-target="#slides_b68a62326bb04179ba859673c3691d90" data-slide-to="3"&gt;
    &lt;/li&gt;&lt;/ol&gt;
    &lt;div class="carousel-inner"&gt;
                &lt;div class="carousel-item active"&gt;&lt;img class="d-block w-100" src="https://blog.solidninja.is/galleries/scala-world-2016/64_scala-world-2016.jpg" alt=""&gt;&lt;/div&gt;
                &lt;div class="carousel-item"&gt;&lt;img class="d-block w-100" src="https://blog.solidninja.is/galleries/scala-world-2016/65_scala-world-2016.jpg" alt=""&gt;&lt;/div&gt;
                &lt;div class="carousel-item"&gt;&lt;img class="d-block w-100" src="https://blog.solidninja.is/galleries/scala-world-2016/66_scala-world-2016.jpg" alt=""&gt;&lt;/div&gt;
                &lt;div class="carousel-item"&gt;&lt;img class="d-block w-100" src="https://blog.solidninja.is/galleries/scala-world-2016/67_scala-world-2016.jpg" alt=""&gt;&lt;/div&gt;
    &lt;/div&gt;
      &lt;a class="carousel-control-prev" href="https://blog.solidninja.is/posts/scala-world-2016/#slides_b68a62326bb04179ba859673c3691d90" role="button" data-slide="prev"&gt;
    &lt;span class="carousel-control-prev-icon" aria-hidden="true"&gt;&lt;/span&gt;
    &lt;span class="sr-only"&gt;Previous&lt;/span&gt;
  &lt;/a&gt;
  &lt;a class="carousel-control-next" href="https://blog.solidninja.is/posts/scala-world-2016/#slides_b68a62326bb04179ba859673c3691d90" role="button" data-slide="next"&gt;
    &lt;span class="carousel-control-next-icon" aria-hidden="true"&gt;&lt;/span&gt;
    &lt;span class="sr-only"&gt;Next&lt;/span&gt;
  &lt;/a&gt;
&lt;/div&gt;

&lt;p&gt;The worked example about doing automated case class migration (using &lt;tt class="docutils literal"&gt;HList&lt;/tt&gt; union and intersection) in particular was both useful and easy to follow.&lt;/p&gt;
&lt;p&gt;&lt;a class="reference external" href="https://github.com/davegurnell/shapeless-guide/blob/develop/dist/shapeless-guide.pdf"&gt;The type astronaut's guide&lt;/a&gt; is currently in development on GitHub.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="a-whirlwind-tour-of-scala-meta"&gt;
&lt;h3&gt;A whirlwind tour of scala.meta&lt;/h3&gt;
&lt;p&gt;Ólafur Páll Geirsson presented a workshop on using &lt;a class="reference external" href="http://scalameta.org/"&gt;scala.meta&lt;/a&gt; to automatically transform scala programs (in one example, by replacing usages of &lt;tt class="docutils literal"&gt;Throwable&lt;/tt&gt; with the &lt;tt class="docutils literal"&gt;NonFatal(_)&lt;/tt&gt; extractor):&lt;/p&gt;
&lt;div class="youtube-video"&gt;
&lt;iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/-l7pV0sFq1c?rel=0&amp;amp;wmode=transparent" frameborder="0" allow="encrypted-media" allowfullscreen&gt;&lt;/iframe&gt;
&lt;/div&gt;&lt;p&gt;At this point the Internet access at the workshop was not very good, but luckily the workshop is available here: &lt;a class="reference external" href="https://olafurpg.github.io/scala.meta-workshop/"&gt;scala.meta-workshop&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The &lt;a class="reference external" href="https://www.youtube.com/watch?v=IPnd_SZJ1nM"&gt;Metaprogramming 2.0&lt;/a&gt; talk is worth watching as well.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="talks-about-the-free-monad"&gt;
&lt;h3&gt;Talks about the &lt;tt class="docutils literal"&gt;Free&lt;/tt&gt; Monad&lt;/h3&gt;
&lt;p&gt;There were a few talks/workshops on the &lt;a class="reference external" href="http://underscore.io/blog/posts/2015/04/14/free-monads-are-simple.html"&gt;Free Monad&lt;/a&gt; which is (and has been) a fairly hot topic in the past year.&lt;/p&gt;
&lt;p&gt;Raul Raja presented a talk &lt;a class="reference external" href="https://speakerdeck.com/raulraja/run-wild-run-free"&gt;Run wild, Run free&lt;/a&gt; which gives a very natural exploration of the problem of composing your computations starting with &lt;tt class="docutils literal"&gt;for&lt;/tt&gt;-comprehensions and exploring until you arrive at the free monad.&lt;/p&gt;
&lt;p&gt;Pawel Szulc also gave a workshop on free monads: &lt;a class="reference external" href="https://github.com/rabbitonweb/make-your-programs-free"&gt;make-your-programs-free&lt;/a&gt;. The core idea is that while actually implementing the interpreter you get to a point where you are wrapping higher-level operations with low-level 'plumbing' code. One way of solving this problem is by definining an interpreter from a higher-level algebra to a lower-level algebra and implementing them separately (which you can see if you follow the commits in the workshop).&lt;/p&gt;
&lt;p&gt;There were plenty of mentions to libraries that help you with free monads: &lt;a class="footnote-reference" href="https://blog.solidninja.is/posts/scala-world-2016/#id11" id="id4"&gt;[1]&lt;/a&gt; &lt;a class="footnote-reference" href="https://blog.solidninja.is/posts/scala-world-2016/#id12" id="id5"&gt;[2]&lt;/a&gt; and it would not be fair to not talk about the downsides of &lt;tt class="docutils literal"&gt;Free&lt;/tt&gt; as well &lt;a class="footnote-reference" href="https://blog.solidninja.is/posts/scala-world-2016/#id13" id="id6"&gt;[3]&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="tuesday-keynote"&gt;
&lt;h3&gt;Tuesday Keynote&lt;/h3&gt;
&lt;p&gt;The keynote on Tuesday was Dick Wall and Josh Suereth talking about all the uses of &lt;tt class="docutils literal"&gt;for&lt;/tt&gt;s: &lt;a class="reference external" href="https://github.com/dickwall/use-the-fors-luke"&gt;Use the fors, Luke&lt;/a&gt;. It started off a super friendly to beginners and at the end also addressed the reasons why perhaps a &lt;tt class="docutils literal"&gt;for&lt;/tt&gt; is not the ultimate tool for Monad composition (shhh, that's a lie). Unfortunately slides/video are not up yet.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="managing-your-resources"&gt;
&lt;h3&gt;Managing your Resources&lt;/h3&gt;
&lt;p&gt;This talk was one of the highlights of the conference for me I think - resource management is one of the problems in Scala that is difficult to solve because of a lack of a &lt;a class="reference external" href="https://doc.rust-lang.org/book/lifetimes.html"&gt;lifetime system&lt;/a&gt; as found in &lt;a class="reference external" href="https://www.rust-lang.org"&gt;Rust&lt;/a&gt; say. And while there are more elegant ways to &lt;tt class="docutils literal"&gt;close()&lt;/tt&gt; your resources after yourself, most of the solutions require either some wrapping construct (such as a &lt;tt class="docutils literal"&gt;for&lt;/tt&gt; or a monad or a simple loan-pattern style closure). It is rare to see such simplicity (in Scala):&lt;/p&gt;
&lt;div class="youtube-video"&gt;
&lt;iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/MV2eJkwarT4?rel=0&amp;amp;wmode=transparent" frameborder="0" allow="encrypted-media" allowfullscreen&gt;&lt;/iframe&gt;
&lt;/div&gt;&lt;p&gt;&lt;a class="reference external" href="https://github.com/densh/talks/blob/master/2016-09-XX-scoped-resource-management.key"&gt;The presentation is available&lt;/a&gt; but it's in Keynote format which GitHub does not have a reader for.&lt;/p&gt;
&lt;p&gt;Here is a working example from the presentation:&lt;/p&gt;
&lt;script src="https://gist.github.com/1348bfb98928ba10ab18c5d7b51ee87f.js"&gt;&lt;/script&gt;&lt;noscript&gt;&lt;pre class="literal-block"&gt;
object MyApp extends App {
  type Resource = java.lang.AutoCloseable

  final class Scope extends Resource {
    final var stack = List.empty[Resource]

    def acquire[R &amp;lt;: Resource](res: R): R = {
      stack = res :: stack
      res
    }

    def close(): Unit = stack match { 
      case Nil =&amp;gt; ()
      case head :: tail =&amp;gt; 
        stack = tail
        try head.close()
        finally close()
    }
  }

  object Scope {
    def apply[T](f: Scope =&amp;gt; T): T = {
      val scope = new Scope
      try f(scope)
      finally scope.close()
    }
  }

  final class SafeWriter()(implicit sc: Scope) extends Resource {
    // vladimir-lu: java.io.PrintWriter is unsafe and should not be used in demos :P
    sc.acquire(this)
    
    private val writer = new java.io.StringWriter()
    println(s"acquired writer '$writer'")

    def write(msg: String): Unit = writer.write(msg)
    
    def close(): Unit = {
      println(s"releasing '$writer'")
      writer.close()
    }
  }

  Scope { implicit sc =&amp;gt; 
    val writer = new SafeWriter()
    writer.write("we want schedules")
    throw new RuntimeException("OOPS")
  }
}
&lt;/pre&gt;
&lt;/noscript&gt;&lt;/div&gt;
&lt;div class="section" id="scalafiddle"&gt;
&lt;h3&gt;ScalaFiddle&lt;/h3&gt;
&lt;p&gt;Scala officially entered the stage of languages with an online interactive sandbox, called &lt;a class="reference external" href="https://scalafiddle.io/sf/n6lR8Xh/2"&gt;ScalaFiddle&lt;/a&gt;. Play with it - it has decent highlighting, speed and libraries that you can use just by clicking and importing.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="tales-from-compiling-to-the-jvm"&gt;
&lt;h3&gt;Tales from Compiling to the JVM&lt;/h3&gt;
&lt;p&gt;This was an interesting talk by Lukas Rytz about certain things that had to change in the Scala 2.12 compiler because of Java 8. Like not initializing static final variables outside the class initialization block... Talk is not up yet unfortunately.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="typelevel-hackday"&gt;
&lt;h2&gt;Typelevel hackday&lt;/h2&gt;
&lt;p&gt;The third day of the conference was a &lt;a class="reference external" href="http://typelevel.org"&gt;Typelevel&lt;/a&gt; hackday which started off with a few talks in the morning. Of these I only took some notes for two of them:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/rabbitonweb/cats_toolbox/blob/master/src/main/scala/ct/Checkout.scala"&gt;Cats in London&lt;/a&gt; - this was Pawel Szulc attempting the what must be by-now infamous interview problem of the Checkout with apples and oranges using &lt;a class="reference external" href="https://github.com/typelevel/cats"&gt;Cats&lt;/a&gt; and the &lt;tt class="docutils literal"&gt;Free&lt;/tt&gt; monad&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/rklaehn/abc"&gt;Array-based collections&lt;/a&gt; - was a nice talk about a collection library that is very different - these ones are immutable and backed by a single flat array&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="other-references"&gt;
&lt;h2&gt;Other References&lt;/h2&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;John de Goes ran an &lt;a class="reference external" href="https://gist.github.com/jdegoes/97459c0045f373f4eaf126998d8f65dc"&gt;Advanced Functional Programming with Scala&lt;/a&gt; training workshop for 2 days prior to the conference and the linked notes look great&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://monix.io"&gt;Monix&lt;/a&gt; was mentioned as a library quite often and I was not fully aware of it before coming to the conference&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So here is the presentation from flatMap Oslo 2016 talking about it:&lt;/p&gt;
&lt;div class="vimeo-video"&gt;
&lt;iframe src="https://player.vimeo.com/video/165922572" width="1280" height="720" frameborder="0" webkitallowfullscreen="webkitAllowFullScreen" mozallowfullscreen="mozallowfullscreen" allowfullscreen="allowFullScreen"&gt;
&lt;/iframe&gt;
&lt;/div&gt;
&lt;p&gt;It was an excellent conference and I look forward to scala.world 2017!&lt;/p&gt;
&lt;table class="docutils footnote" frame="void" id="id11" rules="none"&gt;
&lt;colgroup&gt;&lt;col class="label"&gt;&lt;col&gt;&lt;/colgroup&gt;
&lt;tbody valign="top"&gt;
&lt;tr&gt;&lt;td class="label"&gt;&lt;a class="fn-backref" href="https://blog.solidninja.is/posts/scala-world-2016/#id4"&gt;[1]&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;a class="reference external" href="https://github.com/typelevel/cats/blob/master/docs/src/main/tut/freemonad.md"&gt;https://github.com/typelevel/cats/blob/master/docs/src/main/tut/freemonad.md&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;table class="docutils footnote" frame="void" id="id12" rules="none"&gt;
&lt;colgroup&gt;&lt;col class="label"&gt;&lt;col&gt;&lt;/colgroup&gt;
&lt;tbody valign="top"&gt;
&lt;tr&gt;&lt;td class="label"&gt;&lt;a class="fn-backref" href="https://blog.solidninja.is/posts/scala-world-2016/#id5"&gt;[2]&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;a class="reference external" href="http://perevillega.com/freek-and-free-monads"&gt;http://perevillega.com/freek-and-free-monads&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;table class="docutils footnote" frame="void" id="id13" rules="none"&gt;
&lt;colgroup&gt;&lt;col class="label"&gt;&lt;col&gt;&lt;/colgroup&gt;
&lt;tbody valign="top"&gt;
&lt;tr&gt;&lt;td class="label"&gt;&lt;a class="fn-backref" href="https://blog.solidninja.is/posts/scala-world-2016/#id6"&gt;[3]&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;a class="reference external" href="http://www.slideshare.net/KelleyRobinson1/why-the-free-monad-isnt-free-61836547"&gt;http://www.slideshare.net/KelleyRobinson1/why-the-free-monad-isnt-free-61836547&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;&lt;/div&gt;</description><category>scala</category><category>scala.world</category><category>schedule</category><guid>https://blog.solidninja.is/posts/scala-world-2016/</guid><pubDate>Wed, 21 Sep 2016 10:00:00 GMT</pubDate></item></channel></rss>