<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Marie K. Ekeberg</title>
    <description>The latest articles on DEV Community by Marie K. Ekeberg (@themkat).</description>
    <link>https://dev.to/themkat</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F804143%2F5c5fbe9e-f15f-4221-a71d-22e18d109f31.png</url>
      <title>DEV Community: Marie K. Ekeberg</title>
      <link>https://dev.to/themkat</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/themkat"/>
    <language>en</language>
    <item>
      <title>Become a Maven ninja!</title>
      <dc:creator>Marie K. Ekeberg</dc:creator>
      <pubDate>Mon, 17 Oct 2022 16:43:34 +0000</pubDate>
      <link>https://dev.to/themkat/become-a-maven-ninja-1b26</link>
      <guid>https://dev.to/themkat/become-a-maven-ninja-1b26</guid>
      <description>&lt;p&gt;Today we are going to look at some small tricks that will make you more effective when using Maven! Some of this will be simple, and some of them you might not have heard about. I've done some presentations at work and similar earlier about this, and there is almost always something new to people. The topics range from basic usage and flags, to ways of handling your dependencies. One or two might be a bit enterprise specific, but most can be useful no matter if you program inside a company or not. (no secret that I love those of you who, like myself, LOVE computers with a burning passion! &amp;lt;3 ) &lt;/p&gt;

&lt;p&gt;If you don't know, &lt;a href="https://maven.apache.org/"&gt;Maven&lt;/a&gt; is a build automation tool (dependency management, building, running tests etc.) used for languages on the JVM (Java Virtual Machine). I use it when programming in &lt;a href="https://themkat.net/tags/kotlin.html"&gt;Kotlin&lt;/a&gt; and &lt;a href="https://themkat.net/tags/java.html"&gt;Java&lt;/a&gt;, with the latter becoming less and less frequent (Kotlin rules!!). Several people will probably ask why I'm not using &lt;a href="https://gradle.org/"&gt;Gradle&lt;/a&gt; instead, but that is a discussion for another time.&lt;/p&gt;

&lt;p&gt;There is probably much more that could be covered, but I think this is the most important once you know the basics. If you are new to Maven, suggest starting with &lt;a href="https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html"&gt;an introduction like this&lt;/a&gt;. The guide covers creating projects as well, but many modern frameworks have easier and better generators. Spring Boot have &lt;a href="https://start.spring.io/"&gt;its own initializer website&lt;/a&gt;, and I've made &lt;a href="https://github.com/themkat/spring-starter-terminal"&gt;a CLI version&lt;/a&gt;. Quarkus also have &lt;a href="https://code.quarkus.io/"&gt;a nice web UI you can use to create new projects&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;My last self-promotion before we begin... General command line skills can be very useful when working with almost any command line tool, including Maven. If you need a refresher, I recently wrote &lt;a href="https://themkat.net/2021/10/02/no_nonsense_command_line.html"&gt;a guide to getting started with the command line&lt;/a&gt; (important topics: piping, searching with grep, less).&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic usage and flags
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Check Maven version
&lt;/h3&gt;

&lt;p&gt;I want to show this for one reason, it's useful when checking for possible errors. Simply type &lt;code&gt;mvn -v&lt;/code&gt; and you will get information about the Maven and Java versions currently being used:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NWDj6bEc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m0ta5h39wnjw5a9d26fx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NWDj6bEc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m0ta5h39wnjw5a9d26fx.png" alt="Check Maven version" width="880" height="75"&gt;&lt;/a&gt; &lt;br&gt;
(remember that the Java version can be changed by changing where the environment variable JAVA_HOME points to!)&lt;/p&gt;

&lt;h3&gt;
  
  
  package vs install
&lt;/h3&gt;

&lt;p&gt;You may have noticed that you have a .m2-folder in your home directory. This can contain a settings.xml file (if you have specific local settings like proxies), as well as your local Maven repository (where Maven downloads and stores dependencies and artifacts locally). Why is this important? package builds your project (compiles, run tests etc.), and install does the same in addition to installing it (copying it) to your local Maven repository (.m2/repository). &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NB! As package does not install to .m2 directory you can get some weird issues in multi-module Maven projects with dependencies to each other. Maven reads the dependencies from this local repository, and if it doesn't find it there, it will try the to download it.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  using -f option to run a specific pom file
&lt;/h3&gt;

&lt;p&gt;Just like the title says. If you do something like &lt;code&gt;mvn -f mymodule/pom.xml install&lt;/code&gt;, Maven will build based upon the pom.xml file in the directory mymodule instead of the directory you are currently in. &lt;/p&gt;

&lt;h3&gt;
  
  
  using -X option to get debug information
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;mvn -X goals&lt;/code&gt; (replace goals with the actual Maven goals!) will show you looooots of information you can use when debugging possible issues. Just be aware that this is a lot of information! Ranging from all packages imported, what settings files are used (local for your user as well as global settings for the computer), complete configurations for things like &lt;a href="https://maven.apache.org/surefire/maven-surefire-plugin/"&gt;Surefire&lt;/a&gt; (testing) and so on.&lt;/p&gt;

&lt;p&gt;Now a little story time! I recently helped someone I know with some issues they were having with Maven. All dependencies (within an enterprise) timed out when downloading, and there was a note of a proxy we couldn't find in his local .m2/settings.xml file. How did we find the issue you may ask? Was it some global settings in a parent pom? Well, actually no! Within the first 100 lines of running &lt;code&gt;mvn -X install&lt;/code&gt; we got our answer. A global settings file was put into Mavens install directory (probably by mistake and forgotten). By using the -X option we found the problem, without scratching our heads for days :) &lt;/p&gt;

&lt;h2&gt;
  
  
  Tests
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Run single test(class)
&lt;/h3&gt;

&lt;p&gt;You can run only tests with &lt;code&gt;mvn test&lt;/code&gt;, but did you know you can run just a single test class by using &lt;code&gt;mvn test -Dtest=MyClassTest&lt;/code&gt;? If you have several test classes with that name, use the fully qualified name (including package, example: com.example.MyClassTest).&lt;/p&gt;

&lt;p&gt;Want to run just a single test method within that test class? Just as simple! &lt;code&gt;mvn test -Dtest=MyClassTest#myMethod&lt;/code&gt; will run the test myMethod within your test class.&lt;/p&gt;

&lt;h3&gt;
  
  
  Don't trim stack trace
&lt;/h3&gt;

&lt;p&gt;Sometimes you get unexpected nullpointers or other exceptions within the method you are testing. Some times you will se a very minimal stack trace with just your test method, and the rest cut off (aka trimmed). If you want the complete stack trace with causes, line numbers etc., then you can use the definition &lt;code&gt;-DtrimStackTrace=false&lt;/code&gt;! (works with test, package install, other goals, etc.). Helps me a lot, especially since I prefer to use the command line instead of big bloaty IDEs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Skipping tests
&lt;/h3&gt;

&lt;p&gt;Want to skip all tests when building? &lt;code&gt;mvn install -DskipTests&lt;/code&gt; will run install like usual, but not run any tests (but they will still be compiled!). &lt;/p&gt;

&lt;h2&gt;
  
  
  Dependency plugin
&lt;/h2&gt;

&lt;h3&gt;
  
  
  tree
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;mvn dependency:tree&lt;/code&gt; is one of the most useful commands for checking the dependencies in your project, including transitive dependencies (dependencies of your dependencies and so on). This include all versions of dependencies being used. These reports can be quite long for bigger projects, so it can be useful to pipe the result to less or search it using grep (if you have a suspicion of deprecated code and similar causing weird behavior). &lt;/p&gt;

&lt;h3&gt;
  
  
  analyze
&lt;/h3&gt;

&lt;p&gt;Have a suspicion that you use conflicting versions of packages? Or have unused dependencies in your project? &lt;code&gt;mvn dependency:analyze&lt;/code&gt; can help you here! It will print all info like this. (also useful to browse by piping the output to less!).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NB! Some dependencies in big frameworks like Spring might not be used directly at compile time, so there might be some false positives in the unused packages report.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  display-ancestors
&lt;/h3&gt;

&lt;p&gt;Many enterprises (and maybe open source projects) have hierarchies of &lt;a href="https://maven.apache.org/guides/introduction/introduction-to-the-pom.html"&gt;parent pom files&lt;/a&gt; to handle global settings, approved dependency versions and so on. Sometimes you may want to see what version of parent poms you are currently using (maybe to debug if you are using the correct one, because these things can be updated!). Then &lt;code&gt;mvn dependency:display-ancestors&lt;/code&gt; is your friend! Running it with a new Spring Boot project will show you the version of the Spring Boot starter poms it's using:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9w7mfSLV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dd9sm2mwdfantoqxlr10.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9w7mfSLV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dd9sm2mwdfantoqxlr10.png" alt="Checking Maven parent poms in a Spring project" width="880" height="177"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Versions
&lt;/h2&gt;

&lt;p&gt;Maven have a versions plugin that can be used for several operations relating to versions. The only command here I find useful is &lt;code&gt;mvn versions:set -DnewVersion=1.0.0&lt;/code&gt;. This will set the new versions of your project (including submodules) to 1.0.0. No need to edit the files manually! Neat!&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus: ZSH completion
&lt;/h2&gt;

&lt;p&gt;Oh-My-ZSH has some really useful completions so you don't have to memorize any Maven commands. Just add mvn to your plugins list, open a new tab in your terminal (or new session) and you get nice completion! (you probably need Maven installed first, just to mention it...). Mine looks like this &lt;code&gt;plugins=(git z gh mvn thefuck)&lt;/code&gt; on my personal machine (kubectl is also super useful if you use Kubernetes!).  &lt;/p&gt;

&lt;p&gt;Let's do an example. Let's say I type &lt;code&gt;mvn dependency:&lt;/code&gt; and press tab. Then ZSH will suggest the possible completions for me:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--C3ttbRKM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6rmwd096mljdfht5thw1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--C3ttbRKM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6rmwd096mljdfht5thw1.png" alt="Maven zsh completions" width="880" height="114"&gt;&lt;/a&gt;&lt;br&gt;
(you can off course complete anything, including goals like package, deploy etc. I just wanted to show something that didn't cover my entire window ;) )&lt;/p&gt;

&lt;p&gt;One thing worth mentioning is that this plugin completes the Spring Boot goals like &lt;code&gt;mvn spring-boot:run&lt;/code&gt; and Quarkus goals like &lt;code&gt;quarkus:dev&lt;/code&gt; as well! How cool is that?!?&lt;/p&gt;

</description>
      <category>java</category>
      <category>kotlin</category>
      <category>maven</category>
      <category>commandline</category>
    </item>
    <item>
      <title>The awesomeness of pattern matching in Rust</title>
      <dc:creator>Marie K. Ekeberg</dc:creator>
      <pubDate>Thu, 13 Oct 2022 15:43:27 +0000</pubDate>
      <link>https://dev.to/themkat/the-awesomeness-of-pattern-matching-in-rust-3lbn</link>
      <guid>https://dev.to/themkat/the-awesomeness-of-pattern-matching-in-rust-3lbn</guid>
      <description>&lt;p&gt;If you have followed me for a while, you know that I have really started enjoying Rust in the last year. Rust have many great features, and pattern matching is one of them. If you have used other languages like Haskell or Standard ML, you will notice some similarities. Same with the complete basic pattern matching with &lt;code&gt;when&lt;/code&gt; in Kotlin (with some minor work). The pattern matching in Rust makes for expressive, readable and clear code. I admit that the Rust way of doing it is my personal favorite. In this article we will take a look at this topic, and maybe you will see why I think it's so great!&lt;/p&gt;

&lt;h2&gt;
  
  
  Basics of patterns
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Refutable vs irrefutable
&lt;/h3&gt;

&lt;p&gt;Patterns in Rust come in two types; refutable and irrefutable. Patterns that match conditionally are called refutable, while patterns that match any possible value are called irrefutable. Which one you can use will depend on the context. For example, a let-statement will need a irrefutable pattern, because what would happen if a variable in a let-statement doesn't get a value?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;  &lt;span class="c1"&gt;// Irrefutable patterns&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


  &lt;span class="c1"&gt;// WILL NOT COMPILE&lt;/span&gt;
  &lt;span class="c1"&gt;// let does not allow refutable patterns&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;someString&lt;/span&gt;&lt;span class="py"&gt;.parse&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="c1"&gt;// trying to parse a string will return a Result, and can not guarantee that a result and not an Err is returned&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;if let&lt;/code&gt;-statements on the other hand can have refutable patterns, as the body is evaluated conditionally:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;someString&lt;/span&gt;&lt;span class="py"&gt;.parse&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// ... do something if someString can be parsed as a 32 bit integer ...&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// if let can have a refutable pattern, so we can also use a value for x:&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;someString&lt;/span&gt;&lt;span class="py"&gt;.parse&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// ... do something if someString can be parsed as a 32 bit integer ...&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will see more examples of these kinds of statements and patterns below. If you still think the topic of "refutable vs irrefutable" is hard, reread this section after reading the rest of the article. &lt;a href="https://doc.rust-lang.org/book/ch18-02-refutability.html"&gt;The Rust documentation&lt;/a&gt; also has some examples and explanations that can be worth it to check out.&lt;/p&gt;

&lt;h3&gt;
  
  
  Destructuring
&lt;/h3&gt;

&lt;p&gt;Many patterns are patterns that destructure various types, and they can also be mixed and match together. Let's look at some of them.&lt;/p&gt;

&lt;h4&gt;
  
  
  Tuples
&lt;/h4&gt;

&lt;p&gt;We already saw example of tuple destructuring in the last section, but let's take another look:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;  &lt;span class="c1"&gt;// myTuple is of type (i32, i32, &amp;amp;str)&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;my_tuple&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"hellothere"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;my_str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;my_tuple&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we see in the last line that my_tuple is destructured into 3 new variables: &lt;code&gt;x&lt;/code&gt;, &lt;code&gt;y&lt;/code&gt;, and &lt;code&gt;my_str&lt;/code&gt;. This can be done for all sorts of tuples, as long as the destructured types match.&lt;/p&gt;

&lt;p&gt;You can also match elements with &lt;code&gt;..&lt;/code&gt; (possibly multiple) or &lt;code&gt;_&lt;/code&gt; (single), which are often used to skip elements:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;  &lt;span class="c1"&gt;// ignore my_str&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;my_tuple&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// ignore everything after x&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;my_tuple&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


  &lt;span class="c1"&gt;// bigger tuple&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;bigger_tuple&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// get first and last&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bigger_tuple&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// ambiguous! NOT ALLOWED&lt;/span&gt;
  &lt;span class="c1"&gt;// How would the compiler even know which element you wanted&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;middle&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bigger_tuple&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(notice that the patterns have to be unambiguous)&lt;/p&gt;

&lt;p&gt;You might try the first tuple example above with a tuple struct and get an error message. That is because tuple structs have more in common with struct destructuring, so they require special syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;    &lt;span class="c1"&gt;// Defining a tuple struct that looks like the tuple in the previous example:&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;MyTuple&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


    &lt;span class="c1"&gt;// Destructure it&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;my_tuple&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;TupleStr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"hellothere"&lt;/span&gt;&lt;span class="nf"&gt;.to_string&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nf"&gt;TupleStr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;my_str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;my_tuple&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(notice that we had to change our &lt;code&gt;&amp;amp;str&lt;/code&gt; type to String, as the compiler can't infer the size of any &lt;code&gt;&amp;amp;str&lt;/code&gt; we might want to use in our tuple struct. Strings are saved on the heap, so that solves that problem).&lt;/p&gt;

&lt;p&gt;Tuple structs are technically structs, which brings us to our next type of destructuring...&lt;/p&gt;

&lt;h4&gt;
  
  
  Structs
&lt;/h4&gt;

&lt;p&gt;Structs are not that much different, and an example might show it clearly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;  &lt;span class="c1"&gt;// define a simple struct&lt;/span&gt;
  &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;f32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;f32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;f32&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// create a variable to use&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;myPoint&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;1.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mf"&gt;1.0&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="c1"&gt;// destructure it!&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;Point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;my_point&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// Maybe we just want x and y?&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;Point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;..&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;my_point&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// or maybe just z&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;Point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;..&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;my_point&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One thing you should notice when destructuring structs is that the name have to match the one found in the struct, and that &lt;code&gt;..&lt;/code&gt; have to come last. &lt;code&gt;..&lt;/code&gt; in this case simply means match the rest and ignore the result.&lt;/p&gt;

&lt;h4&gt;
  
  
  Enums
&lt;/h4&gt;

&lt;p&gt;The simplest case for an enum is simply to match one with no data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;  &lt;span class="c1"&gt;// define a simple enum&lt;/span&gt;
  &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;Color&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;Red&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;Blue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;Green&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// match if our color is green&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nn"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Green&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;my_color&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// .. do something is color is green ..&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is not that interesting, as Rust enums are way more powerful. If you are not familiar with them, they can contain data! Let us see an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;  &lt;span class="c1"&gt;// More advanced enum&lt;/span&gt;
  &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;HttpRequest&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nf"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// match the post request&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nn"&gt;HttpRequest&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;my_request&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// .. do something with the post request data ...&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// can also ignore data&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nn"&gt;HttpRequest&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;my_request&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// .. do something when post request ...&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If an enum has several arguments, you can do most of what you are used to from tuples above. You can use ranges (e.g, &lt;code&gt;1..=2&lt;/code&gt;), or skip some elements with &lt;code&gt;..&lt;/code&gt; by itself (e.g, &lt;code&gt;MyEnum(firstElem, .., lastElem)&lt;/code&gt;).&lt;/p&gt;

&lt;h4&gt;
  
  
  Combined
&lt;/h4&gt;

&lt;p&gt;You can also combine all of the above into your own patterns! Structs inside enums, enums inside tuples etc. The list goes on!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;  &lt;span class="c1"&gt;// Define some nested structure&lt;/span&gt;
  &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;Color&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;Red&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;Blue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;Green&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// imagine old OpenGL from the early 2000s where colors of points were interpolated across the shape&lt;/span&gt;
  &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;f32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;f32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;f32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Color&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;Triangle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Point&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Point&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Point&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


  &lt;span class="c1"&gt;// A destructuring based upon the data we want&lt;/span&gt;
  &lt;span class="c1"&gt;// gvet only x for the first point when the first points color is blue&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nf"&gt;Triangle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="n"&gt;Point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Blue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;..&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;my_triangle&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// .. do something with the data we wanted for some reason ..&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Other patterns
&lt;/h3&gt;

&lt;p&gt;There are other types of patterns as well, and these are mostly used with match which you will see more of below. The first one is the or-matcher:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;  &lt;span class="c1"&gt;// matches 1, 2 or 3&lt;/span&gt;
  &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;

  &lt;span class="c1"&gt;// Matches one of the strings&lt;/span&gt;
  &lt;span class="s"&gt;"first"&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="s"&gt;"second"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We saw ranges briefly above:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;  &lt;span class="c1"&gt;// matches 1 to 10 (inclusive)&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;10&lt;/span&gt;

  &lt;span class="c1"&gt;// matches 1 to 10 (non-inclusive)&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;10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(ranges can also be used as indexes for arrays to fetch multiple elements)&lt;/p&gt;

&lt;p&gt;The last pattern I want to show is one used for testing and capturing values. Yes, you can have both! This functionality is often used for capturing values somewhere inside a pattern that satisfy a given condition, and can be used deep inside the pattern. Let us see an example with ranges:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;  &lt;span class="c1"&gt;// Integer version of point&lt;/span&gt;
  &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;MyData&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// Match data x value when it's between 1 and 20 (inclusive)&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;MyData&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;my_x&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;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;..&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;my_data&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// .. do something with the my_x that is 1&amp;lt;=20 ..&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(notice that the variable we use is now called &lt;code&gt;my_x&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;These can be used with if-let and other places that allow refutable patterns, but is mostly used together with other patterns in match. I have rarely, if ever used any of these with if-let (maybe with the exception of ranges). &lt;/p&gt;

&lt;p&gt;That should be the most important topics, but there might be things I've missed. Rust is an evolving language after all, and new features may have been added. There might also be features I don't use as much, and therefore forget about. You can see &lt;a href="https://doc.rust-lang.org/book/ch18-03-pattern-syntax.html"&gt;the Rust documentation on this topic&lt;/a&gt; for more. &lt;/p&gt;

&lt;h3&gt;
  
  
  Caveats
&lt;/h3&gt;

&lt;p&gt;One minor caveat I have found is that Rust is finicky about floating points in patterns. Not about the capturing part, but if you try to use them in ranges and so on. It warns you that it is not supported. We all know how floating points behave, and that they are very sensitive to different scales and that they are rarely (if ever) exact. That probably explains why they are not recommended to use with more advanced Rust patterns.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where to use pattern matching? (example time!)
&lt;/h2&gt;

&lt;p&gt;We have already seen some basic examples above, but let's dive a bit deeper and see how and where to use pattern matching. We have already seen lots of let and if-let statements, so let's look at match and patterns in function signatures!&lt;/p&gt;

&lt;h3&gt;
  
  
  match
&lt;/h3&gt;

&lt;p&gt;This is in my view the switch/case statement you are used to in other languages, but on steroids! match lets you write crazy powerful matchers. We have already looked at all the different patterns above, so let us have some quick examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;  &lt;span class="c1"&gt;// A more extensive version of httprequest&lt;/span&gt;
  &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;HttpRequest&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nf"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="nf"&gt;Put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="n"&gt;Custom&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;Unknown&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// a match expression&lt;/span&gt;
  &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;my_request&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;Get&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="c1"&gt;// .. do something with get ..&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="nf"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nf"&gt;Put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="c1"&gt;// .. do something with the data ..&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(matches have to be exhaustive, and cover all cases. You see we handle this with the wildcard &lt;code&gt;_&lt;/code&gt; above. This is also an advantage, as the compiler helps you remember all cases!)&lt;/p&gt;

&lt;p&gt;match also have another clever thingy called a match guard! It is basically an extra if-sentence inside a case. Let's add it to the second case above:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;   &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;my_request&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="nn"&gt;HttpRequest&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Get&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
           &lt;span class="c1"&gt;// .. do something with get ..&lt;/span&gt;
       &lt;span class="p"&gt;}&lt;/span&gt;
       &lt;span class="nn"&gt;HttpRequest&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nn"&gt;HttpRequest&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;Put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&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;data&lt;/span&gt;&lt;span class="nf"&gt;.is_empty&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
           &lt;span class="c1"&gt;// .. do something with the data ..&lt;/span&gt;
       &lt;span class="p"&gt;}&lt;/span&gt;
       &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
   &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we have added a check to see if the string data sent in is not empty! Pretty nifty!&lt;/p&gt;

&lt;h3&gt;
  
  
  functions
&lt;/h3&gt;

&lt;p&gt;You can use patterns in function definitions! That is really cool, right? You do have to remember that the patterns have to be irrefutable like for let. Don't be sad yet! It still means you can use a lot of destructuring operations we have seen above! Let's look at some examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;  &lt;span class="c1"&gt;// test data&lt;/span&gt;
  &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Vector2d&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;f32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;f32&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// destructured Vector2d&lt;/span&gt;
  &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;length&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Vector2d&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="n"&gt;Vector2d&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;f32&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="nf"&gt;.powi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="nf"&gt;.powi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="nf"&gt;.sqrt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;x_val&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Vector2d&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;..&lt;/span&gt; &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="n"&gt;Vector2d&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;f32&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;x&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;They can be deep as well if you want to, but remember to not f**k up your code's readability.&lt;/p&gt;

&lt;p&gt;Feel free to share any clever pattern matchings you have done in your function signatures in the comments! And yes, I know you have them in JavaScript as well. &lt;/p&gt;




&lt;p&gt;Now you have hopefully seen a little bit of what patterns in Rust can do. If you are new to Rust, maybe it has inspired you to learn the language? :)&lt;/p&gt;

</description>
      <category>rust</category>
      <category>programming</category>
      <category>walkthrough</category>
    </item>
    <item>
      <title>Debugging GameBoy Advance (GBA) programs/games in Emacs</title>
      <dc:creator>Marie K. Ekeberg</dc:creator>
      <pubDate>Tue, 11 Oct 2022 15:45:40 +0000</pubDate>
      <link>https://dev.to/themkat/debugging-gameboy-advance-gba-programsgames-in-emacs-1n87</link>
      <guid>https://dev.to/themkat/debugging-gameboy-advance-gba-programsgames-in-emacs-1n87</guid>
      <description>&lt;p&gt;There are not many articles on debugging GameBoy Advance C code around the web, and most of them seem to be about debugging in VSCode. While VSCode is good, it is far from the awesomness that is Emacs! Debugging GBA code with Emacs is far easier than you would think, and in this article I wills how you how. &lt;/p&gt;

&lt;p&gt;If you have not heard about GDB before, I suggest &lt;a href="https://en.wikipedia.org/wiki/GNU_Debugger"&gt;quickly refreshing the basics&lt;/a&gt; so you don't get confused each time I mention it below :) &lt;/p&gt;

&lt;p&gt;You may already have found some resources related to VSCode around, like &lt;a href="https://github.com/JamieDStewart/GBA_VSCode_Basic"&gt;this repository&lt;/a&gt; and &lt;a href="https://www.gamedev.net/blogs/entry/2268899-gameboy-advance-dev-workflow-in-2020/"&gt;this article&lt;/a&gt;. The launch.json files could be adapter to work with dap-launch (more on that later), but would require some manual work because of several smaller issues (one of them being that Emacs does not support VSCode tasks.json files at the moment). Both of them also seem to require you to copy-paste the launch.json files, and edit them with corrected paths each time (see how you can avoid that in Emacs at the end!). &lt;/p&gt;

&lt;h2&gt;
  
  
  Basic GBA debug workflow (editor independent)
&lt;/h2&gt;

&lt;p&gt;No matter which editor you debug in, the workflow is about the same. You will need &lt;a href="https://devkitpro.org/wiki/Getting_Started"&gt;DevkitPro gba-dev&lt;/a&gt; and an emulator like &lt;a href="https://mgba.io/"&gt;mGBA&lt;/a&gt;. If you want to familiarize yourself with some C code for GBA, as well as Makefiles with CFlags and other build options, there is &lt;a href="https://github.com/devkitPro/gba-examples"&gt;an example repo you can check out&lt;/a&gt; :)&lt;/p&gt;

&lt;p&gt;The process is as follows:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Compile with debug symbols (&lt;code&gt;-g&lt;/code&gt; in DevkitARM gcc). This will generate two files: an elf-file executable with debug symbols, and a standard gba-file (a "normal" executable for GBA which you can also put on any flash card like the EZFlash).&lt;/li&gt;
&lt;li&gt;Start your emulator with gdb debug process. Your emulator will show the visuals here, and will be interacted with using gdb. The best modern emulator in my view is mGBA, where we start the debug process with &lt;code&gt;mGBA --gdb myfile.gba&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Attach to the gdb debug process with DevkitARMs gdb executable using the elf-file.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You could just do it like this without any editor, but your debugging will then happen in the command line, and you will have to remember gdbs commands. This is not as tedious as it sounds, but many of us prefer to see the breakpoints happening, as well as the state, directly in our editors. Many editors therefore provide either a gdb interface, or can interact with it using a debug adapter. You can use debug adapters in VSCode through various plugins, in Emacs with &lt;a href="https://github.com/emacs-lsp/dap-mode"&gt;dap-mode&lt;/a&gt;, in Vim with &lt;a href="https://github.com/puremourning/vimspector"&gt;Vimspector&lt;/a&gt;, and probably in other editors as well. What is written here should be transferable to other editors.&lt;/p&gt;

&lt;p&gt;The above approach should in theory work for both C (and C++) and assembly code, though I have only tried it for C. &lt;/p&gt;

&lt;h2&gt;
  
  
  Adapting it to Emacs with dap-mode
&lt;/h2&gt;

&lt;p&gt;We will use &lt;a href="https://github.com/emacs-lsp/dap-mode"&gt;dap-mode&lt;/a&gt; with the dap-gdb-lldb option here. Under the hood, it uses the debug adapter from the &lt;a href="https://github.com/WebFreak001/code-debug"&gt;Native Debug VSCode extension&lt;/a&gt;. Configuring it is described on &lt;a href="https://emacs-lsp.github.io/dap-mode/page/configuration/#native-debug-gdblldb"&gt;the dap-mode webpages&lt;/a&gt;. After we have configured dap-mode, we could in theory reuse the launch.json configurations from the VSCode related articles above. That will require that you also use lsp-mode, as dap-launch depends on the lsp-workspace-root function and will not resolve when lsp-mode is not used. I don't use lsp-mode with C (&lt;code&gt;company-clang&lt;/code&gt; and &lt;code&gt;company-c-headers&lt;/code&gt; provide what I need), so the next logical solution would be to create a debug template ourselves:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight common_lisp"&gt;&lt;code&gt;  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;dap-register-debug-template&lt;/span&gt; &lt;span class="s"&gt;"GBA debug"&lt;/span&gt;
                               &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;list&lt;/span&gt; &lt;span class="ss"&gt;:name&lt;/span&gt; &lt;span class="s"&gt;"GBA debug"&lt;/span&gt;
                                     &lt;span class="ss"&gt;:type&lt;/span&gt; &lt;span class="s"&gt;"gdbserver"&lt;/span&gt;
                                     &lt;span class="ss"&gt;:request&lt;/span&gt; &lt;span class="s"&gt;"attach"&lt;/span&gt;
                                     &lt;span class="ss"&gt;:gdbpath&lt;/span&gt; &lt;span class="s"&gt;"/opt/devkitpro/devkitARM/bin/arm-none-eabi-gdb"&lt;/span&gt;
                                     &lt;span class="ss"&gt;:target&lt;/span&gt; &lt;span class="s"&gt;":2345"&lt;/span&gt;
                                     &lt;span class="ss"&gt;:cwd&lt;/span&gt; &lt;span class="s"&gt;"/path/to/dir"&lt;/span&gt;
                                     &lt;span class="ss"&gt;:executable&lt;/span&gt; &lt;span class="s"&gt;"file.elf"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Configure cwd and executable to your projects needs, which can also be done by creating one template and configuring it with dap-edit-debug-template. You will off course first need to compile and run your emulator with a gdb process. &lt;/p&gt;

&lt;p&gt;No matter which way you start a debug process, it will look about like this with dap-mode and mgba:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RN6bp0Nh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mpfjrv6pjiuelcr3pov1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RN6bp0Nh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mpfjrv6pjiuelcr3pov1.png" alt="GBA debugging with mGBA in Emacs" width="880" height="431"&gt;&lt;/a&gt;&lt;br&gt;
(here I have added a few breakpoints, which you can toggle with &lt;code&gt;M-x dap-breakpoint-toggle&lt;/code&gt;)&lt;/p&gt;

&lt;h2&gt;
  
  
  Simplifying with gba-debug.el
&lt;/h2&gt;

&lt;p&gt;The solutions above entails some manual work, and that lead me to automate it. Wouldn't it be nice to just run one simple command, and then everything compiles and your debug process just automatically begin? That's exactly what I was thinking, &lt;a href="https://github.com/themkat/emacs-gba-debug"&gt;so I created a simple Emacs extension&lt;/a&gt;. If you use a Makefile to build, and configure the path to mGBA and DevkitARMs gdb, you can debug a GBA program directly with &lt;code&gt;M-x gba-debug-program&lt;/code&gt;. No need to compile manually, run mGBA manually or anything else! &lt;/p&gt;

</description>
      <category>retro</category>
      <category>emacs</category>
      <category>debugging</category>
      <category>dap</category>
    </item>
    <item>
      <title>Emacs Lisp debugging tips</title>
      <dc:creator>Marie K. Ekeberg</dc:creator>
      <pubDate>Mon, 10 Oct 2022 16:56:05 +0000</pubDate>
      <link>https://dev.to/themkat/emacs-lisp-debugging-tips-3igp</link>
      <guid>https://dev.to/themkat/emacs-lisp-debugging-tips-3igp</guid>
      <description>&lt;p&gt;Have you ever gotten weird errors in an Emacs Lisp package? Something like "wrong type argument" or similar shown in the minibuffer? At first glance, these seem kind of cryptic. Where do they come from? Can I get a stack trace? What arguments are functions called with? Today I will show you how to answer these questions!&lt;/p&gt;

&lt;h2&gt;
  
  
  Show stack traces
&lt;/h2&gt;

&lt;p&gt;You have probably gotten one of those weird "wrong type argument" errors, or similar errors just printed in the minibuffer before. You have probably also navigated to the &lt;code&gt;*Messages*&lt;/code&gt;-buffer to check if there is any additional information. Did you know you can see a stack trace instead? Then you can see the functions being called leading up to the error!&lt;/p&gt;

&lt;p&gt;There are several ways to activate it. The most common way is to just set Emacs to enter the debugger when an error happen. You can do this by setting the variable &lt;code&gt;debug-on-error&lt;/code&gt;&lt;br&gt;
 to true (either in your config or just executing it right in your scratch buffer):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight common_lisp"&gt;&lt;code&gt;  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;setq&lt;/span&gt; &lt;span class="nv"&gt;debug-on-error&lt;/span&gt; &lt;span class="no"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I usually prefer to turn it on by need using M-x instead, &lt;code&gt;M-x toggle-debug-on-error&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's show a quick example using a function from &lt;a href="https://github.com/emacs-lsp/lsp-mode"&gt;the lsp-mode codebase&lt;/a&gt;. Experienced programmers will probably notice the issue right away, but let's see how we will debug it. We are going to assume that we have gotten a hash table that we want to fetch results from called &lt;code&gt;result-data&lt;/code&gt;. Running it in the scratch buffer will always show a stack trace, so let's make an interactive function we will call using M-x:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight common_lisp"&gt;&lt;code&gt;  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;defun&lt;/span&gt; &lt;span class="nv"&gt;my-lsp-mode-fun&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;interactive&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;let*&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nv"&gt;my-key&lt;/span&gt; &lt;span class="s"&gt;"key"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
           &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;my-data&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;lsp-get&lt;/span&gt; &lt;span class="nv"&gt;result-data&lt;/span&gt; &lt;span class="nv"&gt;my-key&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
      &lt;span class="c1"&gt;;; ... do something with the data ...&lt;/span&gt;
      &lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we call it without turning on debug-on-error, we will see the error &lt;code&gt;Wrong type argument: symbolp, "key"&lt;/code&gt; in the minibuffer and messages-buffer. Some of you will probably already see that it expects a symbol, not a string. It will be even more clear if you turn on debug-on-error and running it again:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  Debugger entered--Lisp error: (wrong-type-argument symbolp "key")
    symbol-name("key")
    lsp-keyword-&amp;gt;string("key")
    lsp-get(#&amp;lt;hash-table equal 0/65 0x47fa6ca9&amp;gt; "key")
    (let* ((my-key "key") (my-data (lsp-get result-data my-key))))
    my-lsp-mode-fun()
    funcall-interactively(my-lsp-mode-fun)
    call-interactively(my-lsp-mode-fun record nil)
    command-execute(my-lsp-mode-fun record)
    helm-M-x-execute-command(my-lsp-mode-fun)
    helm-execute-selection-action-1()
    helm-execute-selection-action()
    helm-internal((((name . "Emacs Commands history") ...truncated....
    helm-M-x(nil)
    funcall-interactively(helm-M-x nil)
    call-interactively(helm-M-x nil nil)
    command-execute(helm-M-x)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(some data are truncated for better readability by me)&lt;/p&gt;

&lt;p&gt;You see that we get a stack trace of all operations that is done by Emacs. Even if you are not making packages yourself, this can prove very useful to provide information in issue trackers when reporting issues (e.g, on the packages Github repo).&lt;/p&gt;

&lt;p&gt;There are several other ways to toggle errors, ranging from message patterns to signals. You can read more about that &lt;a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Error-Debugging.html"&gt;on the documentation pages&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tracing function invocations
&lt;/h2&gt;

&lt;p&gt;Another useful troubleshooting tips to know is tracing function invocations/calls. Sometimes we just want to see what arguments a specific function is called with, and be notified about it each time it is run. This is done with the =trace-function= command (&lt;code&gt;M-x trace-function&lt;/code&gt;, followed by the name of the function you want to trace). &lt;/p&gt;

&lt;p&gt;Let's reuse the code example from above and trace &lt;code&gt;lsp-get&lt;/code&gt;. After calling our interactive function once, we will then see a buffer called &lt;code&gt;*trace-output*&lt;/code&gt; with data like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  ======================================================================
  1 -&amp;gt; (lsp-get #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8125 data ()) "key")
  1 &amp;lt;- lsp-get: !non-local\ exit!

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Calling it more times will populate the buffer with more data. You can trace as many functions as you want. When you are done tracing a function, you can call &lt;code&gt;untrace-function&lt;/code&gt; and choose the function you want to stop tracing.&lt;/p&gt;

&lt;p&gt;I find this super useful when developing packages. When calling a function external to your package (i.e, from another package or built-in to Emacs Lisp), it's very useful to be able to trace how functions behave. This includes when, or if, they are called, as well as with what arguments.&lt;/p&gt;

</description>
      <category>emacs</category>
      <category>debugging</category>
      <category>productivity</category>
      <category>lisp</category>
    </item>
    <item>
      <title>Packages that make Emacs Lisp more pleasant</title>
      <dc:creator>Marie K. Ekeberg</dc:creator>
      <pubDate>Mon, 10 Oct 2022 16:33:39 +0000</pubDate>
      <link>https://dev.to/themkat/packages-that-make-emacs-lisp-more-pleasant-12cj</link>
      <guid>https://dev.to/themkat/packages-that-make-emacs-lisp-more-pleasant-12cj</guid>
      <description>&lt;p&gt;Emacs Lisp can sometimes seem a little archaic compared to more modern languages. In higher level language we are spoiled with a multitude of easy string handling, list handling, pattern matching and so on. What if I told you that some packages can give you the same ease of use for Emacs Lisp? That they provide more clear APIs, give features you are used to from other languages, and/or abstract away the more tedious details.&lt;/p&gt;

&lt;p&gt;We will mainly look at 3 packages: &lt;a href="https://github.com/magnars/s.el"&gt;s.el&lt;/a&gt;, &lt;a href="https://github.com/rejeep/f.el"&gt;f.el&lt;/a&gt; and &lt;a href="https://github.com/magnars/dash.el"&gt;dash.el&lt;/a&gt;. Two of these packages (first and last) are maintained by &lt;a href="https://github.com/magnars"&gt;Magnar Sveen&lt;/a&gt;, who are also known for &lt;a href="https://emacsrocks.com/"&gt;Emacs Rocks&lt;/a&gt; and &lt;a href="http://whattheemacsd.com/"&gt;What The .emacs.d&lt;/a&gt; (which are still great resources for learning and finding inspiration for your Emacs configuration!). We will also look at &lt;a href="https://github.com/Wilfred/ht.el"&gt;ht.el&lt;/a&gt;. These packages are used a lot in many of the Emacs packages you use in a day to day basis, like &lt;a href="https://github.com/emacs-lsp/lsp-mode"&gt;lsp-mode&lt;/a&gt; and &lt;a href="https://github.com/brotzeit/rustic"&gt;rustic&lt;/a&gt; just to name a few. As most of these already have tons of examples in their READMEs, my main goal of this article is to inspire you to check them out. Hopefully you will know of one new package after reading this article :) &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://github.com/magnars/s.el"&gt;s.el&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/magnars/s.el"&gt;s.el&lt;/a&gt; has a simple mission: to provide more pleasant string handling, and it touts itself as "The long lost Emacs string manipulation library". If you have worked with strings in Emacs Lisp without s.el, it can be a bit tedious. Some functions work like you expect with string inputs, while others expect a list of strings (e.g, &lt;code&gt;string-join&lt;/code&gt;). Others have completely different prefixes (e.g, concat). &lt;/p&gt;

&lt;p&gt;Let's look at some examples of operations from s.el:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight common_lisp"&gt;&lt;code&gt;  &lt;span class="c1"&gt;;; Test if input is a number&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;s-numeric?&lt;/span&gt; &lt;span class="s"&gt;"123"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="c1"&gt;;; =&amp;gt; t&lt;/span&gt;

  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;s-numeric?&lt;/span&gt; &lt;span class="s"&gt;"ab34"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="c1"&gt;;; =&amp;gt; nil&lt;/span&gt;


  &lt;span class="c1"&gt;;; Get the first or last n characters of a string&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;s-left&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="s"&gt;"This is a test"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="c1"&gt;;; =&amp;gt; "This"&lt;/span&gt;

  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;s-right&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="s"&gt;"This is a test"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="c1"&gt;;; =&amp;gt; "test"&lt;/span&gt;


  &lt;span class="c1"&gt;;; Transform to (lower) camel case&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;s-lower-camel-case&lt;/span&gt; &lt;span class="s"&gt;"square function"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="c1"&gt;;; =&amp;gt; "squareFunction"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is &lt;a href="https://github.com/magnars/s.el"&gt;a ton more examples in the github repo for s.el&lt;/a&gt;, so I suggest you check it out if you find it interesting! :) Some of the functions are now provided in newer versions of Emacs (with the &lt;code&gt;string-&lt;/code&gt;-prefix like string-pad-right and string-pad-left), but there are still some operation that Emacs does not provide (e.g, s-left and s-right to get the first or last n characters in a string respectively). &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://github.com/rejeep/f.el"&gt;f.el&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Yes, you read that right: &lt;a href="https://github.com/rejeep/f.el"&gt;f.el&lt;/a&gt;! f.el provides many functions for handling files and directories, including paths and content. While it is certainly possible to do some of these operations directly in standard Emacs Lisp, it takes quite a lot more instructions than the simple ones provided by f. f makes it way more readable, and makes the intent more clear.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight common_lisp"&gt;&lt;code&gt;  &lt;span class="c1"&gt;;; Get the filename of the file (or inner directory)&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;f-filename&lt;/span&gt; &lt;span class="s"&gt;"/home/someuser/Programming/Rust/mos/Cargo.toml"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="c1"&gt;;; =&amp;gt; "Cargo.toml"&lt;/span&gt;


  &lt;span class="c1"&gt;;; Create a directory&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;f-mkdir&lt;/span&gt; &lt;span class="s"&gt;"Programming"&lt;/span&gt; &lt;span class="s"&gt;"Emacs"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="c1"&gt;;; Makes the directories Programming/Emacs&lt;/span&gt;


  &lt;span class="c1"&gt;;; Test if input has a file extension&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;f-ext-p&lt;/span&gt; &lt;span class="s"&gt;"/test/test"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="c1"&gt;;; =&amp;gt; nil&lt;/span&gt;

  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;f-ext-p&lt;/span&gt; &lt;span class="s"&gt;"Cargo.toml"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="c1"&gt;;; =&amp;gt; t&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Like for the other packages, &lt;a href="https://github.com/rejeep/f.el"&gt;f.el provides many examples of usage in its readme&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://github.com/magnars/dash.el"&gt;dash.el&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;If you have ever read source code for any of the Emacs packages you use, you may have noticed operations like &lt;code&gt;-let&lt;/code&gt;, &lt;code&gt;-map&lt;/code&gt;, &lt;code&gt;-lambda&lt;/code&gt;, &lt;code&gt;-&amp;gt;&lt;/code&gt;, and similar ones all starting with a dash. These are from dash.el, and provides quite a lot of nice syntax! Ranging from improved usage and pattern matching (if let expressions like in Rust is available!), to Lisp constructs often found in other languages (like the threading operator &lt;code&gt;-&amp;gt;&lt;/code&gt;). Clojure is probably the biggest inspiration, and this package makes list operations as pleasant as they are in Clojure! (LISP stands for LISt Processing after all!). If you have coded in Emacs Lisp for a while, you may have used †he various cl-functions (e.g, cl-map), but with dash you get more clear and pleasant APIs to do the same and more.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight common_lisp"&gt;&lt;code&gt;  &lt;span class="c1"&gt;;; Flatten a list&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;-flatten&lt;/span&gt; &lt;span class="o"&gt;'&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;))))&lt;/span&gt;
  &lt;span class="c1"&gt;;; =&amp;gt; (1 2 3 4)&lt;/span&gt;


  &lt;span class="c1"&gt;;; Pattern matched let&lt;/span&gt;
  &lt;span class="c1"&gt;;; (my-hash-table is a hash table with keys "name" and "age")&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;-let&lt;/span&gt; &lt;span class="nv"&gt;[&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;&amp;amp;hash&lt;/span&gt; &lt;span class="s"&gt;"name"&lt;/span&gt; &lt;span class="nv"&gt;name&lt;/span&gt; &lt;span class="s"&gt;"age"&lt;/span&gt; &lt;span class="nv"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;my-hash-table]&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;message&lt;/span&gt; &lt;span class="s"&gt;"Hi %s who is %d years old!"&lt;/span&gt; &lt;span class="nv"&gt;name&lt;/span&gt; &lt;span class="nv"&gt;age&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="c1"&gt;;; with an example hash table with name=Nils, and age=56, this will print:&lt;/span&gt;
  &lt;span class="c1"&gt;;; "Hi Nils who is 56 years old!"&lt;/span&gt;

  &lt;span class="c1"&gt;;; There are even pattern matched lambda! -lambda&lt;/span&gt;


  &lt;span class="c1"&gt;;; Thread through the chain using the first argument as the last argument in the next function call etc.&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;-&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;'&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;-flatten&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;message&lt;/span&gt; &lt;span class="s"&gt;"My list: %S"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="c1"&gt;;; prints:&lt;/span&gt;
  &lt;span class="c1"&gt;;; "My list: (1 2 3)"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I LOOOOVE the pattern matching in dash! The &lt;code&gt;-let&lt;/code&gt; variant in dash is probably the functionality in this article I use the most. Threading is also something that can be really nice, and I should probably use it more in places where I usually collect results with let and &lt;code&gt;let*&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/magnars/dash.el"&gt;More examples in the readme&lt;/a&gt; :) &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://github.com/Wilfred/ht.el"&gt;ht.el&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/Wilfred/ht.el"&gt;ht.el&lt;/a&gt; is a library for managing hash tables. While this can be done with the standard library, ht.el makes the intent more clear. They have also &lt;a href="https://github.com/Wilfred/ht.el#why"&gt;written a bit about the why in their readme&lt;/a&gt;. Like they mention, more consistent naming, common prefixes and more, makes coding in Emacs Lisp more pleasant. It's easy to sometimes forget the name of a function, and a common prefix makes it easier to find when using auto completion.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight common_lisp"&gt;&lt;code&gt;  &lt;span class="c1"&gt;;; Create a new hash table&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nv"&gt;my-hash-table&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;ht&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"key"&lt;/span&gt; &lt;span class="s"&gt;"val"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                           &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"other key"&lt;/span&gt; &lt;span class="s"&gt;"other val"&lt;/span&gt;&lt;span class="p"&gt;))))&lt;/span&gt;
    &lt;span class="c1"&gt;;; add another key&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;ht-set!&lt;/span&gt; &lt;span class="nv"&gt;my-hash-table&lt;/span&gt; &lt;span class="s"&gt;"EmacsLisp"&lt;/span&gt; &lt;span class="s"&gt;"More awesome with these libraries!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;;; return the value with the key "MyKey", or "Not found :(" if not found&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;ht-get&lt;/span&gt; &lt;span class="nv"&gt;my-hash-table&lt;/span&gt; &lt;span class="s"&gt;"MyKey"&lt;/span&gt; &lt;span class="s"&gt;"Not found :("&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are also many more utility functions, like accessing nested hash tables, copying hash tables, getting the size and more! Like usual, there are &lt;a href="https://github.com/Wilfred/ht.el"&gt;more examples in the readme&lt;/a&gt; :) &lt;/p&gt;

</description>
      <category>emacs</category>
      <category>editor</category>
      <category>productivity</category>
      <category>lisp</category>
    </item>
    <item>
      <title>Creating container images with Ansible (using ansible-bender)</title>
      <dc:creator>Marie K. Ekeberg</dc:creator>
      <pubDate>Tue, 05 Apr 2022 18:06:12 +0000</pubDate>
      <link>https://dev.to/themkat/creating-container-images-with-ansible-using-ansible-bender-1ff3</link>
      <guid>https://dev.to/themkat/creating-container-images-with-ansible-using-ansible-bender-1ff3</guid>
      <description>&lt;p&gt;Did you know that you can use Ansible to make container images? It's actually a very readable way to create images compared to some Dockerfiles you find online (if you don't think so, you have probably never read one that is 100+ lines). In this article we will look at building images using ansible-bender, which makes for a good way to make images with Ansible with minimal fuzz. &lt;/p&gt;

&lt;p&gt;Need a refresher on Ansible? &lt;a href="https://themkat.net/2022/03/10/intro_to_ansible.html"&gt;It was introduced in the last article, so feel free to read it to refresh the concepts&lt;/a&gt; (at themkat.net). You probably won't get much use out of ansible-bender without knowing the basics of Ansible first.&lt;/p&gt;

&lt;p&gt;In the last article, building container images with Ansible was briefly discussed. &lt;a href="https://blog.tomecek.net/post/building-containers-with-buildah-and-ansible/"&gt;An external blog article on that very subject was linked to&lt;/a&gt;, and if you read it you may have seen a lot of manual steps using Docker- and Buildah-commands. This seems like a lot of boiler plate code just to make container images with Ansible, right? From that idea, &lt;a href="https://ansible-community.github.io/ansible-bender/build/html/index.html"&gt;ansible-bender&lt;/a&gt; was born! Ansible-Bender uses &lt;a href="https://buildah.io/"&gt;Buildah&lt;/a&gt; to build your images, and &lt;a href="https://podman.io/%5D"&gt;Podman&lt;/a&gt; to handle them (some commands like pushing images, build logs, inspecting images etc. are included into Ansible-Bender). Buildah is a container image builder that does not depend on any external programs, and can build containers for any container runtime. A very big downside is that it's only supported on GNU/Linux-based systems, which is bad news if you are using Mac OS X (or god forbid if you are one of the Windows users, ugh). To combat this issue &lt;a href="https://github.com/themkat/dockerfiles/tree/master/ansible-bender"&gt;I made a Docker image&lt;/a&gt; that includes Buildah and all dependencies, that can be used to run Ansible-Bender on other platforms. Might be a bit slower than just using it on a GNU/Linux-based system, but beats not being able to use it at all!&lt;/p&gt;

&lt;p&gt;There are obviously some pros and cons relating to using such a tool compared to plain Dockerfiles:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Can be way more readable than some Dockerfiles you find around the net. Ansible is beautiful to read, so this is a pretty big benefit in my view. Not hating on Dockerfiles, as I think those are fun to write as well. They just get tedious to write if they get too big. &lt;/li&gt;
&lt;li&gt;Already using Ansible playbooks to build your infrastructure? Then you can consider using those, or parts of those, to write containers. &lt;/li&gt;
&lt;li&gt;Imported roles, playbooks etc. can be tested for correct behaviour using tools like Molecule.&lt;/li&gt;
&lt;li&gt;Imported roles, playbooks etc. from Ansible Galaxy can make building images way faster (depending on the type of image you make off course). &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Base images NEEDS to have Python installed. You can do this during runs &lt;a href="https://github.com/ansible-community/ansible-bender/issues/260"&gt;by turning off gather_facts and installing it during playbook run&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Some rough edges, especially with the caching mechanism. Most problems solved by using --no-cache if they occur. Not really a show stopper, as those issues can be fixed by contributors in the future. If you feel like you have what it takes, that could maybe be you? :) &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the end, it comes down to taste. If you really love Ansible and its expressiveness, I think you will enjoy using ansible-bender.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using ansible-bender
&lt;/h2&gt;

&lt;p&gt;So you have installed ansible-bender, either directly or by using an image like mine above. What's next? If you have installed Buildah and Podman as well, you can actually start using the tool directly! If you want a playbook with ansible-bender specific variables inserted for you, you can use the &lt;code&gt;ansible-bender init&lt;/code&gt; command, and a playbook.yml file is created for you. In essence, that is what an ansible-bender playbook really is; a normal Ansible playbook with some special variables. Those special variables are &lt;a href="https://ansible-community.github.io/ansible-bender/build/html/configuration.html"&gt;covered in depth in the documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You may wonder: what if I have ansible_bender variables in more than one play, what happens then? Only the variables from the first play is used. Beyond that you are free to have multiple plays. &lt;/p&gt;

&lt;p&gt;Talk is cheap, let's look at some examples!&lt;/p&gt;

&lt;h3&gt;
  
  
  Example: Containerized Emacs setup
&lt;/h3&gt;

&lt;p&gt;Continuing the trend from the last article! (note: last article &lt;a href="https://themkat.net/2022/03/10/intro_to_ansible.html"&gt;on themkat.net&lt;/a&gt;) Let's set up an image with my Emacs setup ready to be used.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="nn"&gt;---&lt;/span&gt;
&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Emacs setup - Containerized&lt;/span&gt; 
  &lt;span class="na"&gt;hosts&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;all&lt;/span&gt;

  &lt;span class="na"&gt;vars&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;ansible_bender&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;base_image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;python:3.9-slim-bullseye&lt;/span&gt;

      &lt;span class="na"&gt;target_image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;myemacs&lt;/span&gt;
        &lt;span class="na"&gt;cmd&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;bash&lt;/span&gt;
        &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;themkat&lt;/span&gt;
        &lt;span class="na"&gt;working_dir&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/home/themkat&lt;/span&gt;

  &lt;span class="na"&gt;pre_tasks&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Update package archives (Debian-based)&lt;/span&gt;
      &lt;span class="na"&gt;apt&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;update_cache&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
        &lt;span class="na"&gt;cache_valid_time&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;7200&lt;/span&gt;
      &lt;span class="na"&gt;when&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ansible_os_family == "Debian"&lt;/span&gt;

  &lt;span class="na"&gt;tasks&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Make sure Emacs and git is installed&lt;/span&gt;
      &lt;span class="na"&gt;package&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; 
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;emacs&lt;/span&gt;
          &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;git&lt;/span&gt;
        &lt;span class="na"&gt;state&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;present&lt;/span&gt;

    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Create themkat user&lt;/span&gt;
      &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;themkat&lt;/span&gt;
        &lt;span class="na"&gt;create_home&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
        &lt;span class="na"&gt;state&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;present&lt;/span&gt;

    &lt;span class="c1"&gt;# had issues with using the home symbol + become_user here. Might be an issue to look out for&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Download Emacs  config&lt;/span&gt;
      &lt;span class="c1"&gt;#become_user: themkat&lt;/span&gt;
      &lt;span class="na"&gt;git&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;repo&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;https://github.com/themkat/.emacs.d.git&lt;/span&gt;
        &lt;span class="na"&gt;dest&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/home/themkat/.emacs.d&lt;/span&gt;

    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Make themkat the owner&lt;/span&gt;
      &lt;span class="na"&gt;file&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/home/themkat/.emacs.d&lt;/span&gt;
        &lt;span class="na"&gt;owner&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;themkat&lt;/span&gt;
        &lt;span class="na"&gt;recurse&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
        &lt;span class="na"&gt;state&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;directory&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; As you see in the comment above, I have experienced some minor issues with the become_user and home symbol/tilde. These might be bugs in ansible-bender or relating to containers. Have not tested enough to find the culprit yet, so presenting this as something to be aware of. &lt;/p&gt;

&lt;p&gt;How do we build this? Use the command &lt;code&gt;ansible-bender build playbook.yml&lt;/code&gt;. You might wonder why we have the "when Debian"-check when it will always be Debian with that base image. The reason is simple: by doing this we keep the setup flexible. If we wanted a Fedora image, we could simple override it from the command line &lt;code&gt;ansible-bender build playbook.yml fedora:35&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;As you can see above, the differences from a plain Ansible playbook to a ansible-bender one used to create images are not that big. Simple some image related variables like the base image, as well as resulting image name, command to be run, user etc. Standard settings we do when making container images in general.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example: Simple Spring Boot application image
&lt;/h3&gt;

&lt;p&gt;Let's do something slightly different by demonstrating the usage of volumes and Ansible roles during build. It's not unusual to want to package an application into a container, and for simplicity we put it into the same directory as the playbook itself (so we can use the playbook_dir variable). To not make this example too advanced, we simply use a Spring Boot application made from &lt;a href="https://start.spring.io/"&gt;Spring Initializr&lt;/a&gt; without any changes (&lt;a href="https://github.com/themkat/spring-starter-terminal"&gt;if you prefer a terminal UI, there is one for that too&lt;/a&gt;). We'll use geerlingguys java role to setup Java to not boggle down the example with Java setup.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="nn"&gt;---&lt;/span&gt;
&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Containerized Spring Boot app&lt;/span&gt;
  &lt;span class="na"&gt;hosts&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;all&lt;/span&gt;
  &lt;span class="na"&gt;vars&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;ansible_bender&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;base_image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;fedora:35&lt;/span&gt;
      &lt;span class="na"&gt;target_image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;cmd&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;java -jar /app/spring-app.jar&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;spring-app-example&lt;/span&gt;
      &lt;span class="na"&gt;working_container&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;{{&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;playbook_dir&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;}}:/src"&lt;/span&gt;

  &lt;span class="na"&gt;roles&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;role&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;geerlingguy.java&lt;/span&gt;
      &lt;span class="na"&gt;java_packages&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;java-11-openjdk&lt;/span&gt;

  &lt;span class="na"&gt;tasks&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Make app directory that will include our application&lt;/span&gt;
    &lt;span class="na"&gt;file&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/app&lt;/span&gt;
      &lt;span class="na"&gt;state&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;directory&lt;/span&gt;

  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Build application&lt;/span&gt;
    &lt;span class="na"&gt;command&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;chdir&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/src&lt;/span&gt;
      &lt;span class="na"&gt;cmd&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;./mvnw clean install&lt;/span&gt;
      &lt;span class="na"&gt;creates&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/src/target/spring-app-0.0.1-SNAPSHOT.jar&lt;/span&gt;

  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Copy jar file to app directory&lt;/span&gt;
    &lt;span class="na"&gt;copy&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;remote_src&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
      &lt;span class="na"&gt;src&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/src/target/spring-app-0.0.1-SNAPSHOT.jar&lt;/span&gt;
      &lt;span class="na"&gt;dest&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/app/spring-app.jar&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To run this one, we will have to use ansible-galaxy to install geerlingguy.java first, either with a requirements file or directly (&lt;code&gt;ansible-galaxy install geerlingguy.java&lt;/code&gt;). After that we can simply build our image like in the last example.&lt;/p&gt;

&lt;p&gt;What happens here? We build our Maven Spring Boot project, and put the resulting jar file into a app-directory. You may wonder: Shouldn't we delete the contents of src? Won't all the source code and build files be included in the final image? No! When building is done, src is unmounted and the directory is empty. Pretty neat!&lt;/p&gt;

&lt;h3&gt;
  
  
  Useful commands
&lt;/h3&gt;

&lt;p&gt;ansible-bender has quite &lt;a href="https://ansible-community.github.io/ansible-bender/build/html/usage.html#getting-logs-of-a-build"&gt;a few useful commands beyond just building images&lt;/a&gt;. If you want to fetch earlier build logs, that can be done. Inspecting images is simple. But to me, the most useful is probably pushing images. Not necessarily to a central repository or something, but to Docker. I have to admit, I still use Docker. Let's say we have built the myemacs image as in the first example, and want to push it to our docker daemon with the tag 0.1. Then we simply run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  ansible-bender push docker-daemon:myemacs:0.1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;In summary, ansible-bender seems like a promising tool that definitely have some fun and useful use cases. Making images with Ansible seems like a very readable way to make images, especially for bigger ones. Dockerfiles quickly gets messy, and Ansible might be an antidote for that issue. &lt;/p&gt;

</description>
      <category>ansible</category>
      <category>docker</category>
      <category>containers</category>
      <category>buildah</category>
    </item>
    <item>
      <title>Bit tricks - Absolute value without branching</title>
      <dc:creator>Marie K. Ekeberg</dc:creator>
      <pubDate>Thu, 17 Feb 2022 19:18:04 +0000</pubDate>
      <link>https://dev.to/themkat/bit-tricks-absolute-value-without-branching-5ba</link>
      <guid>https://dev.to/themkat/bit-tricks-absolute-value-without-branching-5ba</guid>
      <description>&lt;p&gt;Bit trickery is always interesting! Sometimes you can use them to avoid branching (like if-checks), other times they are useful to save a few CPU cycles to avoid expensive operations. The absolute value trick I will show you here, is mostly to avoid branching. Why would you want to avoid branching? Many newer processors, from the mid 90s and onward, do something called &lt;a href="https://en.wikipedia.org/wiki/Instruction_pipelining"&gt;pipelining&lt;/a&gt; to achieve a form of instruction level parallelism. While a classic processor fetch an instruction, decode it, then execute, a pipelined processor can fetch the next instruction while the previous one is decoded. You can have multiple operations like this almost in parallel. If we have to branch, like for an if-check, we might not have fetched the correct next instruction anymore, and might have to fetch new ones (for the whole pipeline). This can be an expensive operation. Calculating absolute values is a problem where we often have to have a branch (an if-check for smaller than 0).&lt;/p&gt;

&lt;p&gt;It should be noted that modern computers like the X86, modern phones, modern ARM processors (like the ones in M1 Macs) etc. have branch prediction and are very fast. This makes bit trickery like the ones in this article unnecessary, and it will have little to no effect. The same is true for the Java Virtual Machine (JVM) which already does a lot of optimizations for you.&lt;/p&gt;

&lt;p&gt;Hopefully you remember what the absolute value is. It is simply a positive representation of the value, in other words it's the value without its sign. -10 will be 10, and 10 will still be 10. Mathematically we can define it like this:&lt;/p&gt;

&lt;p&gt;

&lt;/p&gt;
&lt;div class="katex-element"&gt;
  &lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;∣x∣={xif x≥0−xif x&amp;lt;0
\begin{equation}
|x| = \begin{cases}
  x &amp;amp; \text{if } x \geq 0 \\
  -x &amp;amp; \text{if } x &amp;lt; 0
\end{cases}
\end{equation}
&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mtable"&gt;&lt;span class="col-align-c"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;∣&lt;/span&gt;&lt;span class="mord mathnormal"&gt;x&lt;/span&gt;&lt;span class="mord"&gt;∣&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="minner"&gt;&lt;span class="mopen delimcenter"&gt;&lt;span class="delimsizing size4"&gt;{&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mtable"&gt;&lt;span class="col-align-l"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;x&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;−&lt;/span&gt;&lt;span class="mord mathnormal"&gt;x&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="arraycolsep"&gt;&lt;/span&gt;&lt;span class="col-align-l"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord text"&gt;&lt;span class="mord"&gt;if &lt;/span&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;x&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;≥&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord text"&gt;&lt;span class="mord"&gt;if &lt;/span&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;x&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="tag"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="eqn-num"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;


&lt;h2&gt;
  
  
  Naive implementation with branching
&lt;/h2&gt;

&lt;p&gt;We can implement the mathematical formula above directly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;absoluteValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(we could also have used the ternary operator above, but decided to use a plain if for clarity)&lt;/p&gt;

&lt;p&gt;The implementation explains itself. Now onto the clever trick!&lt;/p&gt;

&lt;h2&gt;
  
  
  The bit trick
&lt;/h2&gt;

&lt;p&gt;For integers, the trick can be described like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;absoluteValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;mask&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;31&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="n"&gt;mask&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;mask&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(can be done inline if you prefer to. Useful if you want to avoid the stack page for the function call. You might have a clever compiler already doing this for you though...)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note! If you are compiling for multiple processors, with different bit sizes for int, you can use sizeof to calculate the byte-size of int and use the result to calculate the bit shift length (remember to subtract 1 from the total size!).&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;But wait... How does it work? First of all, let's note that we work with signed integers here, which means that the bit shift preserves the sign bit (0 for positive, 1 for negative). &lt;/p&gt;

&lt;p&gt;Let's see what happens for a positive number (and also 0):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Mask becomes 0 (sign bit is propagated down to the lowest bit place)&lt;/li&gt;
&lt;li&gt;Input XOR 0 becomes the input itself.&lt;/li&gt;
&lt;li&gt;The input minus 0, is still the input itself.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now let's take a negative number:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Mask becomes -1 (bit shift 31 places. Sign bit propagated, so the new empty places to the left is filled with the sign bit 1). This is the same as 1111 1111 1111 1111 1111 1111 1111 1111 for the integer above. (Read up on &lt;a href="https://en.wikipedia.org/wiki/Two%27s_complement"&gt;two complement&lt;/a&gt; and how negative numbers are handled if this seems foreign to you).&lt;/li&gt;
&lt;li&gt;Input XOR -1 becomes the (bit-)negated version of the input (the same as using the NOT operator on the input)&lt;/li&gt;
&lt;li&gt;The result of the previous operation minus -1, which is the same as adding 1, becomes the same as multiplying the input variable by -1. Remember that negated value + 1 is how we get the negative version of our current number in the two complement system.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You might now see the pattern here. For positive numbers (and 0), nothing happens, we just return the input number. For negative numbers, we negate them and add 1, which is the same as creating the negative version of our number in the two complement system.&lt;/p&gt;

&lt;p&gt;If that still sounds like magic to you, let's break it down further! You can probably skip these sections if you understand the trick already. &lt;/p&gt;

&lt;h3&gt;
  
  
  (Bit) Negation if negative number
&lt;/h3&gt;

&lt;p&gt;All bits in the mask will either be 0 or 1, so let's take a look at a XOR table for a single mask bit:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;A&lt;/th&gt;
&lt;th&gt;B&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Let's say B is the mask in this case. Take a close look. For 0 mask, the input (A) stays the same, while for the 1 mask it is negated! (in other words: imagine using the NOT operator on A only when B is 1). &lt;/p&gt;

&lt;h3&gt;
  
  
  Two complement
&lt;/h3&gt;

&lt;p&gt;We can easily see that for a positive number, we get the same number back (positive number XOR 0 - 0 is still the same number). What happens for negative ones? The number is first negated, then we subtract negative one. Wait a minute... This is the same as negating and then adding one! Which is how we negate a number in the two complement system. This is how negative numbers are represented. That's the magic of this trick :)&lt;/p&gt;

&lt;h2&gt;
  
  
  Where can this be useful?
&lt;/h2&gt;

&lt;p&gt;While not super useful for modern high powered hardware, it might be useful in other contexts. Some of these are low power microcontrollers, retro computers and retro console programming. Some processors in these sort of devices are low powered, and sometimes we might need some clever tricks. The absolute value trick might prove useful where we simply want to avoid the extra cycles the jump call and conditional require, even though it shines the brightest in cases of pipelined processors. &lt;/p&gt;

</description>
      <category>c</category>
      <category>programming</category>
      <category>retrocomputing</category>
      <category>binary</category>
    </item>
    <item>
      <title>Spring Boot - Easy customizations for your applications!</title>
      <dc:creator>Marie K. Ekeberg</dc:creator>
      <pubDate>Sat, 29 Jan 2022 12:55:55 +0000</pubDate>
      <link>https://dev.to/themkat/spring-boot-easy-customizations-for-your-applications-aei</link>
      <guid>https://dev.to/themkat/spring-boot-easy-customizations-for-your-applications-aei</guid>
      <description>&lt;p&gt;In this article I will show you two types of easy customizations you can do with your Spring Boot applications. One of them can be very useful, and one is mostly cosmetic for the benefit of those seeing the server logs. This article is language agnostic, so it can be used for all programming languages you might make Spring Boot applications in (probably Java or Kotlin).  &lt;/p&gt;

&lt;h2&gt;
  
  
  Custom banner
&lt;/h2&gt;

&lt;p&gt;When you start your Spring Boot application you will see a banner like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5AZze3xZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m82kwlexag9yo9crbg26.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5AZze3xZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m82kwlexag9yo9crbg26.png" alt="Spring Boot basic banner" width="880" height="297"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is pretty standard, and maybe a bit boring. Did you know that you can create your own? Just create a banner.txt file in your src/main/resources directory, and it will show at startup! Those of you who want to do more advanced colors, formatting, showing versions etc. might wonder how that is done? There are several variables for that, some are documented in a great way in &lt;a href="https://gist.github.com/tremaineeto/b3918287ed3128ac1d4f1a2f1ef6c39d"&gt;this Github Gist I found&lt;/a&gt;. Other formatting includes &lt;a href="https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/ansi/AnsiColor.html"&gt;AnsiColor&lt;/a&gt; and &lt;a href="https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/ansi/AnsiStyle.html"&gt;AnsiStyle&lt;/a&gt; (links are for the enums found in the current version of Spring Boot). Using a collection of these and some ascii art (generated by &lt;a href="https://patorjk.com/software/taag/"&gt;this website&lt;/a&gt;) we can create a cool banner for our very cool server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  ${AnsiColor.GREEN} ________  ________  ________  ___               ________  _______   ________  ___      ___ _______   ________     
  |\   ____\|\   __  \|\   __  \|\  \             |\   ____\|\  ___ \ |\   __  \|\  \    /  /|\  ___ \ |\   __  \    
  \ \  \___|\ \  \|\  \ \  \|\  \ \  \            \ \  \___|\ \   __/|\ \  \|\  \ \  \  /  / | \   __/|\ \  \|\  \   
   \ \  \    \ \  \\\  \ \  \\\  \ \  \            \ \_____  \ \  \_|/_\ \   _  _\ \  \/  / / \ \  \_|/_\ \   _  _\  
    \ \  \____\ \  \\\  \ \  \\\  \ \  \____        \|____|\  \ \  \_|\ \ \  \\  \\ \    / /   \ \  \_|\ \ \  \\  \| 
     \ \_______\ \_______\ \_______\ \_______\        ____\_\  \ \_______\ \__\\ _\\ \__/ /     \ \_______\ \__\\ _\ 
      \|_______|\|_______|\|_______|\|_______|       |\_________\|_______|\|__|\|__|\|__|/       \|_______|\|__|\|__|
                                                     \|_________|                                                    

                                        ${AnsiStyle.BOLD}${AnsiColor.RED}- Spring Boot Version ${spring-boot.formatted-version} -${AnsiStyle.NORMAL}
  ${AnsiColor.DEFAULT}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we see some of the codes in action! So how might this look? Like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mKLo_x1B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ma8c8kcz587eivu9u4nf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mKLo_x1B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ma8c8kcz587eivu9u4nf.png" alt="Spring Boot custom banner" width="880" height="192"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you are the more boring type, you can also remove the banner with a yaml property:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;    &lt;span class="na"&gt;spring&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;main&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;show_banner&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="no"&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Static web pages
&lt;/h2&gt;

&lt;p&gt;Now time for the most known feature in this article: hosting static webpages with Spring Boot. You may or may not know that files found in src/main/resources/static are hosted as webpages when running your application? This means you can make an index.html file that is served when accessing the root path of your server in the browser. Try making one, and run mvn spring-boot:run (if you use Maven), and you can access it at localhost:8080 (unless a different URL is configured). To show you an example, here is an index.html file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;  &lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;My awesome little website!&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Welcome to this awesome server!&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;br&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"#"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Documentation&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we can open the browser, and voila!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oSq0ssgi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/itz50wh617csmqeyaplf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oSq0ssgi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/itz50wh617csmqeyaplf.png" alt="Spring Boot hosting static site" width="681" height="233"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What are these websites useful for? Other than being fun to have, off course. Some ideas for information you might want to show:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Maybe you have some documentation that the user of your server might want to read? Swagger pages, SOAP WSDL files, API documentation for possible integration code, link to relevant web pages to get people up to speed with relevant concepts etc.&lt;/li&gt;
&lt;li&gt;URLs of other APIs your servers communicate with, unless they should be masked (some more proprietary solutions still do this). Most applications don't like in a vacuum anymore, and you might use different URLs in test vs production (which you will see fast if shown on a site like this).&lt;/li&gt;
&lt;li&gt;Build information like versions, user who last committed changes etc.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html"&gt;Spring Boot Actuator&lt;/a&gt; link (these are sometimes configured to be other places than the default)&lt;/li&gt;
&lt;li&gt;Spring Boot Actuator calls to provide information directly in the site (might require some JavaScript). Monitoring etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So they are not just for the developers having fun, they can provide real value as well! &lt;/p&gt;

&lt;p&gt;You can also provide your own error web pages, with the most famous example being 404 errors. This is as easy as what we did above, with one simple tweak: The sites need to be in the error-subdirectory (inside your src/main/resources/static directory). So your 404 page would be at this path src/main/resources/static/error/404.html.&lt;/p&gt;

&lt;p&gt;If you want to make more advanced web pages, this can be done with something like &lt;a href="https://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html"&gt;Thymeleaf&lt;/a&gt; (templating system), JSP, or just with some JavaScript code. You would probably want to have something like stylesheets and other files included to make your sites more fancy as well. The site above was kept simple for the sake of example (and for you to have something to copy-paste to test quickly if you want to!).&lt;/p&gt;




&lt;p&gt;I focused on more general tips that applies to most Spring Applications in this article. There are off course more customizations (that are more dependent on your problem domain) that can be done, including &lt;a href="https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html#actuator.endpoints.implementing-custom"&gt;custom Actuator endpoints&lt;/a&gt; and &lt;a href="https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.application.failure-analyzer"&gt;custom FailureAnalyzer&lt;/a&gt; (when application fails to start), but these are topics for a separate article.&lt;/p&gt;

&lt;p&gt;Do you have any other fun customizations for Spring Boot? Feel free to mention them in the comments!&lt;/p&gt;

</description>
      <category>java</category>
      <category>kotlin</category>
      <category>springboot</category>
      <category>spring</category>
    </item>
    <item>
      <title>Often forgotten features in Kotlin</title>
      <dc:creator>Marie K. Ekeberg</dc:creator>
      <pubDate>Fri, 28 Jan 2022 19:28:16 +0000</pubDate>
      <link>https://dev.to/themkat/often-forgotten-features-in-kotlin-11pl</link>
      <guid>https://dev.to/themkat/often-forgotten-features-in-kotlin-11pl</guid>
      <description>&lt;p&gt;This time we will look at features in Kotlin that are often overlooked in walktroughs or other places, but are still cool and can be useful! Some are features some of us might know of, but often forget exist. Others are features we forget exist untill we see them mentioned, probably because we don't use them as often (or very rarely). Hopefully you will see at least one feature you have forgotten existed! &lt;/p&gt;

&lt;h2&gt;
  
  
  Inline objects
&lt;/h2&gt;

&lt;p&gt;The most known usage of inline/anonymous objects in Kotlin is in places where we need to send in an object (in the Java sense of the word) that implements an interface, the particular object is only needed once in your code, and a lambda won't suffice (because the interface is not functional, i.e, it has several methods). That was probably a bit confusing if you haven't seen it before. It's hard finding good examples of this, as it doesn't happen that often (probably happens more often on Android with its click handlers and more). Let us just use Runnable as a quick example, even though you could easily have used a lambda:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;  &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;myThread&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;object&lt;/span&gt; &lt;span class="err"&gt;: &lt;/span&gt;&lt;span class="nc"&gt;Runnable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Using an anonymous inner class/object"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you may ask: how is this a forgotten feature? It's not like seeing this is rare! You are right, that's why I started this section with "the most known usage of...". Now to the feature we often forget is possible. You can actually create variables that are just objects, almost just like you are used to in JavaScript!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;      &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;myObj&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;object&lt;/span&gt; &lt;span class="err"&gt;{
          val name = "somename"
          val pair = get&lt;/span&gt;&lt;span class="nc"&gt;RandomPoint&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
          &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;somelist&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;listOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Element"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Another element"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myObj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myObj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pair&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myObj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;somelist&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also have functions, but if you don't implement an interface, those methods won't be visible outside the object. The exceptions to this rules are the methods you find in the Any class, which you can also override inside the inline/anonymous object (e.g, toString). There are a few other limitations as well. One particular, frustrating one, was that I could not destructure the pair above, and access the elements:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;  &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;myObj&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;object&lt;/span&gt; &lt;span class="err"&gt;{
      val (x,y) = get&lt;/span&gt;&lt;span class="nc"&gt;RandomPoint&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// NOT ALLOWED! :( &lt;/span&gt;
  &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myObj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myObj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This may be fixed in future versions of Kotlin. You also can't use inline objects at the top level in a file, and they have to be used inside a function or class. (at least from my experience in scripts using KScript).&lt;/p&gt;

&lt;p&gt;Now you may ask: Where is this last feature useful? It might be useful as return values in private methods of classes, or as working storage for variables to group them. I think in general we should avoid this for bigger projects, as it may hinder readability a bit as the project grows. For scripts in Kotlin on the other hand, I think it can prove useful to keep the code concise and create logical groupings. That is where I see the biggest use for it. &lt;/p&gt;

&lt;h2&gt;
  
  
  tailrec
&lt;/h2&gt;

&lt;p&gt;Let's start with the simplest recursive implementation of factorial:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;  &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;*&lt;/span&gt;&lt;span class="nf"&gt;factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n-1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember that every time a recursive call is done, a new stack page is created with the values of the variables (here the new value of n) which consumes more memory. In the above implementation, we see that there will be many such new stack pages depending on the value of n. We also need to backtrack once the recursive calls have finished (i.e, reached the base case in the if-check) and calculate the multiplications.&lt;/p&gt;

&lt;p&gt;How can we improve? With the concept of tail recursion. What is tail recursion? Tail recursion is a compiler optimization (or in other languages like Scheme, interpreter optimization) that is done to reuse the stack page each time, and avoid the backtracking needed by classic recursive functions. When these conditions are met, you don't get a recursive process anymore, but an iterative one (just like for regular loops). Sounds awesome right? You will &lt;em&gt;NEED&lt;/em&gt; to write your functions in a way that they don't need backtracking for this to work, so a lot of the responsibility is on YOU! After you have written your function in such a way, you can use the tailrec keyword. One way of implementing factorial this way is by using a optional argument with a default value (maybe write in docs how this works, especially if it is meant to be used by others!):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;    &lt;span class="k"&gt;tailrec&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n-1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;*&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is only one possible way, and you can probably find some better ways of solving this. &lt;/p&gt;

&lt;p&gt;You may wonder; what happens if I use the tailrec keyword without meeting the condition above, in that the last call is a tail call (i.e, no backtracking needed). You will get a compiler warning saying that the recursive call is not a tail call, simple as that :)&lt;/p&gt;

&lt;h2&gt;
  
  
  Extension functions to lambdas
&lt;/h2&gt;

&lt;p&gt;Lambdas are instances of classes behind the scenes, which means that you can create extension functions on them! If you never thought about it, this may seem confusing. What would the class name look like? Let's create one type of lambda (taking in two integers, and returning a new integer), and then later creating an extension function for it. First a simple add function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;  &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;add&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;num2&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
      &lt;span class="n"&gt;num1&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;num2&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Take a close look at the type of the add-function. That is the class we can create extension functions for! Let's create a new extension function that returns the same function, but the execution will start with a simple print statement:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;  &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="err"&gt;((&lt;/span&gt;&lt;span class="nf"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;logged&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;num2&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
          &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Calling function ${this.toString()}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
          &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;num2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can see it in use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;addLogged&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;logged&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"2+3 = ${addLogged(2,3)}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can guess, the output will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  Calling function (kotlin.Int, kotlin.Int) -&amp;gt; kotlin.Int
  2+3 = 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This was a very simple example, and there are more fun things you can do with this functionality. Maybe you want to create a memoized version of your function? Or create compositions of functions (e.g, create a =f(g(x))= function for two functions f and g)? Look around, especially in the realm of functional programming, and you might find more interesting use cases.&lt;/p&gt;

&lt;h2&gt;
  
  
  Delegation of properties/variables
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Recap of delegation in classes
&lt;/h3&gt;

&lt;p&gt;Most people know of the class-level delegation, but in case you don't, let's do a very quick recap. When extending a class or implementing an interface, the relationship is of type "is a" (e.g, a Toyota is a Car, a Siberian Husky is a Dog etc.). Composition, storing the relationship in a variable, is of the type "has a" (e.g, Manager manages/has a programmer do work for them, Cat has a human which gives them food etc.). Delegation is a special case of composition where we delegate work to another class. You probably don't want your Cat to implement the Human interface just to be able to get Food...? You want to delegate the work to the cats servant/owner. How do this look in Kotlin?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;  &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Human&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// usually the implementation would be implementation specific&lt;/span&gt;
      &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;getFood&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Getting food"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// implementations that in the real world probably implements their own getFood&lt;/span&gt;
  &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;GrownUp&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Human&lt;/span&gt;

  &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;name&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;servant&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Human&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Human&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="n"&gt;servant&lt;/span&gt;


  &lt;span class="c1"&gt;// usage&lt;/span&gt;
  &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;me&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;GrownUp&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;myCat&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Mittens"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;me&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;myCat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getFood&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This looks just like the Cat implements the Human interface, but behind the scenes it delegates the work to a Human. That is the power of delegation for classes, and why it is often called a special form of composition (as the usage is a bit different then basic composition).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NB!&lt;/strong&gt; Just to make it clear. The example above is very simple to illustrate the point. If you still feel unsure about it and want to see more examples, I suggest &lt;a href="https://kotlinlang.org/docs/delegation.html"&gt;looking into the official documentation&lt;/a&gt;. This was only meant as a recap to prepare you for the main point, so I assumed you just needed a refresher :) &lt;/p&gt;

&lt;h3&gt;
  
  
  properties/variables
&lt;/h3&gt;

&lt;p&gt;The delegation example above was probably familiar to you, as it is presented clearly in many Kotlin texts. Did you know that you can use delegates on properties/variables as well? This is a simple way of adding some extra functionality around the given type. Let's say you want a variable with a number, but it can only be even. Or a string that can only be lower case. Let's see how a stupid example like that can be implemented:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;  &lt;span class="c1"&gt;// Even number&lt;/span&gt;
  &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EvenNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;num&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;operator&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;getValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;thisRef&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;?,&lt;/span&gt; &lt;span class="n"&gt;prop&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;KProperty&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="err"&gt;*&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="k"&gt;operator&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;setValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;thisRef&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;?,&lt;/span&gt; &lt;span class="n"&gt;prop&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;KProperty&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="err"&gt;*&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;,&lt;/span&gt; &lt;span class="n"&gt;newValue&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;newValue&lt;/span&gt; &lt;span class="p"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
              &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newValue&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
              &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newValue&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;evenNum&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="nc"&gt;EvenNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;evenNum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;evenNum&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;
  &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;evenNum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;evenNum&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
  &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;evenNum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="c1"&gt;// string always lower case&lt;/span&gt;
  &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LowerCaseString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;str&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;operator&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;getValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;thisRef&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;?,&lt;/span&gt; &lt;span class="n"&gt;prop&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;KProperty&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="err"&gt;*&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="k"&gt;operator&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;setValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;thisRef&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;?,&lt;/span&gt; &lt;span class="n"&gt;prop&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;KProperty&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="err"&gt;*&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;,&lt;/span&gt; &lt;span class="n"&gt;newValue&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="n"&gt;str&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lowercase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;myStr&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="nc"&gt;LowerCaseString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello there"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myStr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;myStr&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"hi"&lt;/span&gt;
  &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myStr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;myStr&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"MY HANDS ARE TYPING WORDS"&lt;/span&gt;
  &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myStr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You may notice that the delegates don't need to implement an interface, but they still need to implement the above methods (values/vals probably don't need the setter). &lt;/p&gt;

&lt;p&gt;Now let us see what the output looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  23
  10
  2
  Hello there
  hi
  my hands are typing words
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;NB!&lt;/strong&gt; You may notice that the setValue is not invoked on the constructor, which is something I want you to be cautious of. If you want to enforce it at that point, I would suggest using a explicit constructor or a init-block.&lt;/p&gt;

&lt;p&gt;These examples are a little bit stupid on purpose, but they show some simple validation use cases for delegates. Now that you know how they work, you can probably think of more fun stuff to use it for :) &lt;/p&gt;

&lt;h2&gt;
  
  
  Honorable mention
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Referential equality using ===
&lt;/h3&gt;

&lt;p&gt;Let's say you have implemented the equals method, and it checks for structural equality (e.g, the fields are equal to each other). What if you want to check that they are the same object, do we need to remove the equals-method? NO! You can use === instead, and you check if the objects are the same objects in memory (i.e, same reference).&lt;/p&gt;

</description>
      <category>kotlin</category>
    </item>
  </channel>
</rss>
