<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Oleksandr Manenko&apos;s Blog</title>
    <description>Software engineering tips and tricks.</description>
    <link>https://manenko.com/</link>
    <atom:link href="https://manenko.com/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Mon, 25 Mar 2024 16:35:58 +0000</pubDate>
    <lastBuildDate>Mon, 25 Mar 2024 16:35:58 +0000</lastBuildDate>
    <generator>Jekyll v4.3.3</generator>
    
      <item>
        <title>The principle of type substitution</title>
        <description>&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Type substitution (or Liskov Substitution Principle in object-oriented contexts) allows an object of a certain type (supertype, superclass) to be replaced with an object of another type (subtype, subclass).&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Let&amp;#8217;s clarify the difference between &lt;em&gt;subclass&lt;/em&gt; and &lt;em&gt;subtype&lt;/em&gt; (and their &lt;em&gt;super-&lt;/em&gt; counterparts) first. They are closely related, but stem from different concepts: inheritance and type compatibility.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;subtype-vs-subclass&quot;&gt;Subtype vs. Subclass&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Subclass is a class that inherits from another class, known as its superclass. A subclass inherits methods and properties (behavior and state) from the superclass and can add new methods and properties or override existing ones.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Subtype is a broader concept that refers to a type that satisfies the &lt;em&gt;contract&lt;/em&gt; or interface of another type, known as its supertype. A subtype guarantees that it can be used in any context where its supertype is expected, adhering to the Liskov Substitution Principle (LSP). This relationship is not limited to classes and inheritance but also applies to interfaces, traits, and other type constructs.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;supertype-vs-superclass&quot;&gt;Supertype vs. Superclass&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Superclass is the class from which a subclass inherits. It&amp;#8217;s a specific entity in class-based OOP that provides a &lt;em&gt;blueprint&lt;/em&gt; for creating subclasses.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Supertype is a type that is generalized or abstracted from its subtypes. A supertype defines a contract (set of operations, methods, properties) that all its subtypes must satisfy. The subtype-supertype relationship can be determined by inheritance in OOP, but it can also arise from other type relationships, such as implementing an interface (Java) or conforming to a protocol (Clojure).&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;type-substitution&quot;&gt;Type substitution&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;The key aspect of this concept is that the substituting object (the subtype instance) must be able to perform all the duties that the original object (the supertype instance) could perform, without requiring any changes to the external code that uses the object.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;In order to achieve this level of compatibility, the following rules must apply:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;ulist&quot;&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;The subtype must implement all the methods and properties that the supertype defines.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The subtype must not alter the expected behavior of the supertype in a way that could break the client code: preconditions, postconditions, and invariants must remain the same. For example, a subtype must not throw exceptions not expected from the supertype methods.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;java-example&quot;&gt;Java example&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Java supports classical inheritance, making it straightforward to demonstrate subtyping.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;java&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;abstract&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;tok-nc&quot;&gt;Animal&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;abstract&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nf&quot;&gt;makeSound&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;throws&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;AnimalSoundException&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;tok-kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;tok-nc&quot;&gt;AnimalSoundException&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;extends&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;Exception&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;public&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nf&quot;&gt;AnimalSoundException&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;tok-kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;tok-nc&quot;&gt;Dog&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;extends&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;Animal&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-nd&quot;&gt;@Override&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nf&quot;&gt;makeSound&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;throws&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;AnimalSoundException&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;Woof!&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;tok-kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;tok-nc&quot;&gt;Cat&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;extends&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;Animal&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-nd&quot;&gt;@Override&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nf&quot;&gt;makeSound&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;throws&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;AnimalSoundException&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;Meow!&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;tok-kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;tok-nc&quot;&gt;Rhino&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;extends&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;Animal&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-nd&quot;&gt;@Override&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nf&quot;&gt;makeSound&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;throws&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;AnimalSoundException&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;throw&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;RuntimeException&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;Rhinos are silent and deadly!&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;tok-kd&quot;&gt;public&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;tok-nc&quot;&gt;Main&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;public&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;Animal&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;animals&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;Animal&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;Dog&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;Cat&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;()};&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;Animal&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;animal&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;animals&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;            &lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;                &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;animal&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-na&quot;&gt;makeSound&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;            &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;catch&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;AnimalSoundException&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;                &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-na&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;Error occurred: &amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-na&quot;&gt;getMessage&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;());&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;            &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Here, &lt;code&gt;Dog&lt;/code&gt;, &lt;code&gt;Cat&lt;/code&gt;, and &lt;code&gt;Rhino&lt;/code&gt; have the same method &lt;code&gt;makeSound&lt;/code&gt; as their supertype. However, only &lt;code&gt;Dog&lt;/code&gt; and &lt;code&gt;Cat&lt;/code&gt; adhere to LSP. &lt;code&gt;Rhino&lt;/code&gt; violates the behavior of its supertype by throwing a &lt;code&gt;RuntimeException&lt;/code&gt; and breaks the logic used by the client code which process instances of &lt;code&gt;Animal&lt;/code&gt;-compatible types.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;python-example&quot;&gt;Python example&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;We could have used inheritance for Python as well but let&amp;#8217;s demonstrate subtyping via different means: duck typing.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;tok-nc&quot;&gt;AnimalSoundException&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-ne&quot;&gt;Exception&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;tok-k&quot;&gt;pass&lt;/span&gt;

&lt;span class=&quot;tok-k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;tok-nc&quot;&gt;Dog&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;tok-k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;tok-nf&quot;&gt;make_sound&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;tok-nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-s2&quot;&gt;&amp;quot;Woof!&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;tok-nc&quot;&gt;Cat&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;tok-k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;tok-nf&quot;&gt;make_sound&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;tok-nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-s2&quot;&gt;&amp;quot;Meow!&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;tok-nc&quot;&gt;Rhino&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;tok-k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;tok-nf&quot;&gt;make_sound&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;tok-k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;tok-ne&quot;&gt;RuntimeError&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-s2&quot;&gt;&amp;quot;Rhinos are silent!&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;tok-nf&quot;&gt;let_it_speak&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;animal&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;tok-k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;tok-n&quot;&gt;animal&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;make_sound&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;tok-k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;AnimalSoundException&lt;/span&gt; &lt;span class=&quot;tok-k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;tok-nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;tok-s2&quot;&gt;&amp;quot;Error occurred: &lt;/span&gt;&lt;span class=&quot;tok-si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;tok-s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-n&quot;&gt;animals&lt;/span&gt; &lt;span class=&quot;tok-o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;Dog&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;Cat&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;Rhino&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;()]&lt;/span&gt;
&lt;span class=&quot;tok-k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;animal&lt;/span&gt; &lt;span class=&quot;tok-ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;animals&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;tok-n&quot;&gt;let_it_speak&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;animal&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;&lt;code&gt;Rhino&lt;/code&gt; violates the supertype&amp;#8217;s behavior even though it commits to the same interface.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;variance&quot;&gt;Variance&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Variance is a type system concept that describes how the subtyping relationship between more complex types relates to the subtyping relationship between their component types. It&amp;#8217;s mostly relevant in the context of generic types. Variance determines whether a generic type with a certain component type (like &lt;code&gt;List&amp;lt;String&amp;gt;&lt;/code&gt;) can be considered a subtype or supertype of the same generic type with a different component type (like &lt;code&gt;List&amp;lt;Object&amp;gt;&lt;/code&gt;). There are three main kinds of variance: covariance, contravariance, and invariance.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;sect2&quot;&gt;
&lt;h3 id=&quot;covariance&quot;&gt;Covariance&lt;/h3&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Covariance allows for type substitution in a &lt;em&gt;same direction&lt;/em&gt;. This means that if type &lt;code&gt;A&lt;/code&gt; is a subtype of type &lt;code&gt;B&lt;/code&gt;, then &lt;code&gt;Container&amp;lt;A&amp;gt;&lt;/code&gt; is considered a subtype of &lt;code&gt;Container&amp;lt;B&amp;gt;&lt;/code&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Covariance is applied in contexts where it is safe to read items from a container, but not to write items into it, because every item read out of a &lt;code&gt;Container&amp;lt;A&amp;gt;&lt;/code&gt; is guaranteed to be at least an instance of &lt;code&gt;B&lt;/code&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Consider a system where we need to read or extract information from a collection of objects. With covariance, we can use a collection of a more specific type where a collection of a more general type is expected. This is particularly useful when dealing with read-only operations where the type safety of write operations is not a concern.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Let&amp;#8217;s use Java for this example, focusing on a scenario involving a collection of messages and the task of reading these messages.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;First, we define message classes:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;java&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;abstract&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;tok-nc&quot;&gt;Message&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;abstract&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nf&quot;&gt;getInfo&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;tok-kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;tok-nc&quot;&gt;TextMessage&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;extends&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;Message&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;private&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;TextMessage&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-na&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-nd&quot;&gt;@Override&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nf&quot;&gt;getInfo&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;TextMessage: &amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;tok-kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;tok-nc&quot;&gt;ImageMessage&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;extends&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;Message&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;private&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;imageUrl&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ImageMessage&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;imageUrl&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-na&quot;&gt;imageUrl&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;imageUrl&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-nd&quot;&gt;@Override&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nf&quot;&gt;getInfo&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;ImageMessage with URL: &amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;imageUrl&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Next, we define a &lt;code&gt;MessageReader&lt;/code&gt; interface with a covariant generic type:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;java&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;tok-nc&quot;&gt;MessageReader&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;extends&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;Message&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nf&quot;&gt;readMessage&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Suppose we want a reader that specifically reads &lt;code&gt;TextMessage&lt;/code&gt; objects from a source. We&amp;#8217;ll create a &lt;code&gt;TextMessageReader&lt;/code&gt;:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;java&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;tok-nc&quot;&gt;TextMessageReader&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;implements&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;MessageReader&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;TextMessage&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-nd&quot;&gt;@Override&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;public&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;TextMessage&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nf&quot;&gt;readMessage&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;TextMessage&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;Hello from TextMessageReader&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Finally, to demonstrate covariance, we&amp;#8217;ll use a method that expects a &lt;code&gt;MessageReader&amp;lt;Message&amp;gt;&lt;/code&gt;, but we&amp;#8217;ll pass it a &lt;code&gt;MessageReader&amp;lt;TextMessage&amp;gt;&lt;/code&gt; thanks to covariance:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;java&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;public&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;tok-nc&quot;&gt;MessageReaderDemo&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-c1&quot;&gt;// A method that expects a reader capable of reading messages.&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;public&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nf&quot;&gt;displayMessageInfo&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;MessageReader&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;extends&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;Message&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;reader&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;Message&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;reader&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-na&quot;&gt;readMessage&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-na&quot;&gt;getInfo&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;());&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;public&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-c1&quot;&gt;// Despite expecting a MessageReader&amp;lt;Message&amp;gt;, we can pass a&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-c1&quot;&gt;// MessageReader&amp;lt;TextMessage&amp;gt; thanks to covariance.&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;TextMessageReader&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;textReader&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;TextMessageReader&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;displayMessageInfo&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;textReader&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;In this example, &lt;code&gt;displayMessageInfo&lt;/code&gt; method is designed to work with a &lt;code&gt;MessageReader&lt;/code&gt; that can read any &lt;code&gt;Message&lt;/code&gt;. Thanks to covariance (&lt;code&gt;MessageReader&amp;lt;? extends Message&amp;gt;&lt;/code&gt;), we can pass it a &lt;code&gt;TextMessageReader&lt;/code&gt;, which is a &lt;code&gt;MessageReader&lt;/code&gt; specialized for &lt;code&gt;TextMessage&lt;/code&gt; objects. This is safe because the method only reads messages from the reader and doesn&amp;#8217;t attempt to write or modify them, ensuring type safety.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Covariance allows us to substitute a generic type with a subtype in situations where it&amp;#8217;s safe to do so, typically in read-only contexts. In our case, a &lt;code&gt;MessageReader&lt;/code&gt; specialized for a subtype (&lt;code&gt;TextMessage&lt;/code&gt;) can be used wherever a &lt;code&gt;MessageReader&lt;/code&gt; for the supertype (&lt;code&gt;Message&lt;/code&gt;) is expected.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect2&quot;&gt;
&lt;h3 id=&quot;contravariance&quot;&gt;Contravariance&lt;/h3&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Contravariance allows for type substitution in an &lt;em&gt;opposite direction&lt;/em&gt;. This means that if type &lt;code&gt;A&lt;/code&gt; is a subtype of type &lt;code&gt;B&lt;/code&gt;, then &lt;code&gt;Container&amp;lt;B&amp;gt;&lt;/code&gt; is considered a subtype of &lt;code&gt;Container&amp;lt;A&amp;gt;&lt;/code&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Contravariance is useful when dealing with types of functions (or more generally, producers and consumers of data). In a contravariant scenario, we can substitute a type with one of its supertypes. This is the opposite of covariance, where we can substitute a type with one of its subtypes.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Consider a function as a box that does something with input. If you have a function that expects a specific type of input (say, an &lt;code&gt;Animal&lt;/code&gt;), you can safely replace it with a function that works with a more general input (say, a &lt;code&gt;LivingCreature&lt;/code&gt;, of which &lt;code&gt;Animal&lt;/code&gt; is a subtype).&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Why is this safe? Because anywhere the original function was used, it was expected to handle an &lt;code&gt;Animal&lt;/code&gt;. The new function can handle not just &lt;code&gt;Animal&lt;/code&gt; but any &lt;code&gt;LivingCreature&lt;/code&gt;, making it more flexible without breaking the original contract.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Imagine we have a system that processes messages, and we have a function type that handles these messages:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;java&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;tok-nc&quot;&gt;MessageHandler&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nf&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Let&amp;#8217;s reuse &lt;code&gt;Message&lt;/code&gt; and &lt;code&gt;TextMessage&lt;/code&gt; types from the previous example. Then &lt;code&gt;TextMessageHandler&lt;/code&gt; can be defined as follows:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;java&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;tok-nc&quot;&gt;TextMessageHandler&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;implements&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;MessageHandler&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;TextMessage&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-nd&quot;&gt;@Override&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;public&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nf&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;TextMessage&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;Handling text message: &amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-na&quot;&gt;getContent&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;());&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;If &lt;code&gt;MessageHandler&lt;/code&gt; were contravariant, you could use a &lt;code&gt;MessageHandler&amp;lt;Message&amp;gt;&lt;/code&gt; wherever a &lt;code&gt;MessageHandler&amp;lt;TextMessage&amp;gt;&lt;/code&gt; is expected. This means you could substitute a handler that is capable of dealing with all kinds of messages, not just text messages, which could look like this:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;java&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;tok-nc&quot;&gt;GenericMessageHandler&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;implements&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;MessageHandler&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;Message&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-nd&quot;&gt;@Override&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;public&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nf&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;Message&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;Handling a message: &amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-na&quot;&gt;getInfo&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;());&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;This substitution is safe because the &lt;code&gt;GenericMessageHandler&lt;/code&gt; can handle everything that &lt;code&gt;TextMessageHandler&lt;/code&gt; can (since &lt;code&gt;TextMessage&lt;/code&gt; is a subtype of &lt;code&gt;Message&lt;/code&gt;), plus possibly more:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;java&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;tok-nc&quot;&gt;MessageProcessor&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-c1&quot;&gt;// Process a list of messages with a handler&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-c1&quot;&gt;// that can handle a supertype of those messages&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;public&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nf&quot;&gt;processMessages&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;extends&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;Message&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;messages&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;MessageHandler&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;&amp;lt;?&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;Message&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;handler&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;Message&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;messages&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;            &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;handler&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-na&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;public&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;Message&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;messages&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-na&quot;&gt;of&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;TextMessage&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;Hello, World!&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ImageMessage&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;http://example.com/omnomnom.jpg&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;));&lt;/span&gt;

&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-c1&quot;&gt;// Using the generic handler to process a mixed list of messages&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;GenericMessageHandler&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;genericHandler&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;GenericMessageHandler&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;processMessages&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;messages&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;genericHandler&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;The principle here is that it&amp;#8217;s safe to write (or in this case, handle) something more general in a place that expects something more specific. Contravariance is applicable in situations where data flows into a component (like function arguments).&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect2&quot;&gt;
&lt;h3 id=&quot;invariance&quot;&gt;Invariance&lt;/h3&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Invariance means there is no subtype relationship between different instantiations of the same generic type, regardless of their component types&apos; relationship. Neither &lt;code&gt;Container&amp;lt;A&amp;gt;&lt;/code&gt; nor &lt;code&gt;Container&amp;lt;B&amp;gt;&lt;/code&gt; is considered a subtype of the other, even if &lt;code&gt;A&lt;/code&gt; is a subtype of &lt;code&gt;B&lt;/code&gt;. Both reading from and writing to an invariant container are type-safe, but this comes at the cost of flexibility.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Type substitution deals with how objects of different types can be used interchangeably without breaking the functionality of a program. It can be implemented through various mechanisms, such as inheritance, interfaces, generic, or duck typing, depending on the programming language.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;When using generics, type substition becomes somewhat more complicated and determines its rules via a concept called variance:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;ulist&quot;&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Covariance allows substituting a type with its subtype, ideal for read-only scenarios.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Contravariance permits a type to be replaced with its supertype, suitable for write operations.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Invariance disallows substituting a generic type with its subtypes or supertypes.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;</description>
        <pubDate>Sun, 24 Mar 2024 00:00:00 +0000</pubDate>
        <link>https://manenko.com/2024/03/24/the-principle-of-type-substitution.html</link>
        <guid isPermaLink="true">https://manenko.com/2024/03/24/the-principle-of-type-substitution.html</guid>
        
        <category>programming</category>
        
        <category>computer science</category>
        
        <category>type theory</category>
        
        
      </item>
    
      <item>
        <title>How to use Rust memory allocator from C</title>
        <description>&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;The Rust allocator API requires a &lt;a href=&quot;https://doc.rust-lang.org/1.76.0/core/alloc/layout/struct.Layout.html&quot;&gt;&lt;code&gt;Layout&lt;/code&gt;&lt;/a&gt; struct to be passed into each of its functions. For example, to reallocate or free memory, you need to pass the pointer to the existing memory block and the layout used during its allocation.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;This is a problem when interacting with C API which does not provide any means of storing additional allocation data.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;The Cassandra C++ driver&amp;#8217;s memory allocation functions is the example of such API, e.g.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;unsafe&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;extern&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;C&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;tok-nf&quot;&gt;realloc&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ptr&lt;/span&gt;: &lt;span class=&quot;tok-o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;c_void&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;size&lt;/span&gt;: &lt;span class=&quot;tok-kt&quot;&gt;usize&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;-&amp;gt; &lt;span class=&quot;tok-o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;c_void&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;See? The memory reallocation function has the same signature as &lt;code&gt;realloc&lt;/code&gt; from the standard C library and takes a pointer and the new size of the block.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Now compare this to the Rust memory allocation API:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;unsafe&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;tok-nf&quot;&gt;realloc&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ptr&lt;/span&gt;: &lt;span class=&quot;tok-o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kt&quot;&gt;u8&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;layout&lt;/span&gt;: &lt;span class=&quot;tok-nc&quot;&gt;Layout&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;new_size&lt;/span&gt;: &lt;span class=&quot;tok-kt&quot;&gt;usize&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;-&amp;gt; &lt;span class=&quot;tok-o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kt&quot;&gt;u8&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;This function has one additional parameter: the layout, I was talking about earlier. This must be the same layout used during the memory allocation.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;And here comes the problem: we have no information about the layout in the C API and thus cannot pass it to Rust.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Oh, well, back to the old good memory tricks we used to use in C in the old days.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;We need to store the layout used to allocate a memory block in the block itself, i.e. we need to allocate extra bytes, write the layout at the beginning of the block, and return the pointer to the first byte that follows the layout data. For example, when the driver requests 60 bytes of memory we allocate the memory block as follows:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;text&quot;&gt;&lt;span&gt;&lt;/span&gt;
                     requested amount of bytes (60)
                                    |
   size_of::&amp;lt;usize&amp;gt; bytes (8)       |
                |                   |
                v                   v
             +----+----------------------------------+
     +------&amp;gt;| 68 |0000000000000000000000000000000000|
     |       +----^----------------------------------+
block size        |
                  |
   pointer we return to the caller&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Then, for Rust &lt;a href=&quot;https://doc.rust-lang.org/1.76.0/alloc/alloc/fn.realloc.html&quot;&gt;&lt;code&gt;realloc&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://doc.rust-lang.org/1.76.0/alloc/alloc/fn.dealloc.html&quot;&gt;&lt;code&gt;dealloc&lt;/code&gt;&lt;/a&gt; functions, we take the pointer, subtract the size of the allocation information, read the size of the block and reconstruct the layout structure. We don&amp;#8217;t need to store the alignment, as we always use the same alignment (C API does not provide alignment parameters at all anyway).&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;The implementation could be found &lt;a href=&quot;https://github.com/manenko/cassander/blob/9c5b0f1fa1deee31759a1e424496411dce802e49/src/allocator.rs&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;</description>
        <pubDate>Tue, 19 Mar 2024 00:00:00 +0000</pubDate>
        <link>https://manenko.com/2024/03/19/how-to-use-rust-memory-allocator-from-c.html</link>
        <guid isPermaLink="true">https://manenko.com/2024/03/19/how-to-use-rust-memory-allocator-from-c.html</guid>
        
        <category>programming</category>
        
        <category>C</category>
        
        <category>Rust</category>
        
        
      </item>
    
      <item>
        <title>Нова країна</title>
        <description>&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Ця історія почалася десять років тому, коли я вперше інвестував у біткоїн. Я був програмістом, що був сфокусований на високих технологіях, але завжди мріяв про великі зміни у світі. Коли ціна біткоїна досягла небувалих висот, я зрозумів: &lt;em&gt;це мій шанс&lt;/em&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Продавши все що мав, я отримав неймовірну суму грошей. Втім, замість того щоб інвестувати їх у традиційні активи або витрачати на розкоші, я вирішив реалізувати свою давню мрію — створити ідеальне суспільство.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Далі я вирушив на пошуки потрібного місця і знайшов маленький, майже непомітний острів біля екватора. Він був гарним та майже забутий цим світом. Мені навіть довелося найняти приватного детектива, щоб знайти власника.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Власником виявилася людина, що на той момент жила в Парагваї, та мала свої, особливі стосунки із законом. Цей чоловік як раз сидів у вʼязниці під велику заставу, то ж було нескладно вмовити його передати мені острів в обмін на кругленьку суму легальних грошей.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Купивши острів, я витратив частину коштів на розвиток інфраструктури та встановив відновлювальні джерел енергії. Ми будували житлові будинки, школи та лікарні, використовуючи екологічно чисті матеріали та технології.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Втім моя мета була не просто створити ще одну країну. Я мріяв побудувати анархічне суспільство, засноване на принципах свободи та рівності. Люди самі вирішували як їм жити, без централізованого уряду, який би нав&amp;#8217;язував свої правила.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Економіка була заснована на принципах взаємодопомоги та взаємного кредитування. Використовуючи блокчейн, ми створили власну цифрову валюту для спрощення обміну товарами та послугами, а також цифрову систему прийняття рішень, щоб важливі питанння вирішувалися через голосування всіма членами спільноти.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Ще через десять років наш острів перетворився на справжній рай. Місцеві, які спочатку ставилися до мене насторожено, тепер називали мене Падре через те, що вважали мене батьком нової нації. А ще тому, що час почав забирати своє, додавши мені сивого волосся.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Ми створили суспільство, де немає місця злочинності та заздрості, а відносини між людьми будуються на взаємній повазі та допомозі. Все це стало можливим завдяки вірі в ідею, що світ можна змінити на краще та запасу біткоїну, вартість якого стрімко вирісла, перш ніж знову впасти.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;А потім я прокинувся і згадав, що немає в мене стільки біткоїну. Блять.&lt;/p&gt;
&lt;/div&gt;</description>
        <pubDate>Mon, 05 Feb 2024 00:00:00 +0000</pubDate>
        <link>https://manenko.com/2024/02/05/new-country.html</link>
        <guid isPermaLink="true">https://manenko.com/2024/02/05/new-country.html</guid>
        
        <category>твір</category>
        
        <category>ukrainian</category>
        
        
      </item>
    
      <item>
        <title>Sparse sets</title>
        <description>&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;A sparse set is a simple data structure that has a few interesting properties:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;ulist&quot;&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;O(1) to add an item.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;O(1) to remove an item.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;O(1) to lookup an item.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;O(1) to clear the set.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;O(n) to iterate over the set.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;A set does not require its internal items storage to be initialised upon creation (!).&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Sparse sets are commonly used to implement &lt;a href=&quot;https://en.wikipedia.org/wiki/Entity_component_system&quot;&gt;Entity Component System&lt;/a&gt; architectural pattern.
I plan to cover ECS in one of my future posts but for now let&amp;#8217;s try to understand and implement sparse sets.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;overview&quot;&gt;Overview&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Sparse sets use two integer arrays internally: &lt;code&gt;dense&lt;/code&gt; and &lt;code&gt;sparse&lt;/code&gt;.
The former is a packed array that stores the set&amp;#8217;s items (integers) in the insertion order.
The latter is an array that can have holes (hence the name – &lt;code&gt;sparse&lt;/code&gt;) and it maps the set&amp;#8217;s items to their indices in &lt;code&gt;dense&lt;/code&gt;.
The set also keeps track of how many items it has.
We call it &lt;code&gt;N&lt;/code&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Sparse sets could be implemented as growable, i.e. being able to reallocate the memory they use but I will use non-growable version in this post for simplicity.
This means a user has to specify the size of the set upon creation.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-cm&quot;&gt;/** An item stored in a sparse set. */&lt;/span&gt;
&lt;span class=&quot;tok-k&quot;&gt;typedef&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-kt&quot;&gt;uint32_t&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;rho_ss_id&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;tok-cm&quot;&gt;/** Maximum possible value of the rho_ss_id. */&lt;/span&gt;
&lt;span class=&quot;tok-k&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;rho_ss_id&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;rho_ss_id_max&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;UINT32_MAX&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;tok-cm&quot;&gt;/** The sparse set. */&lt;/span&gt;
&lt;span class=&quot;tok-k&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nc&quot;&gt;rho_ss&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;rho_ss_id&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;sparse&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-cm&quot;&gt;/**&amp;lt; Sparse array used to speed-optimise the set.     */&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;rho_ss_id&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;dense&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;&lt;span class=&quot;tok-cm&quot;&gt;/**&amp;lt; Dense array that stores the set&amp;#39;s items.         */&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;rho_ss_id&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt;       &lt;/span&gt;&lt;span class=&quot;tok-cm&quot;&gt;/**&amp;lt; Number of items in the dense array.              */&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;rho_ss_id&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt;     &lt;/span&gt;&lt;span class=&quot;tok-cm&quot;&gt;/**&amp;lt; Maximum allowed item ID.                         */&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;};&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;an-empty-set&quot;&gt;An empty set&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;We start with an empty set that can have up to &lt;code&gt;max&lt;/code&gt; items.
The next picture demonstrates the sparse set that can hold up to ten items.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;imageblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;img src=&quot;/images/sparse-sets/sparse-set-empty.png&quot; alt=&quot;An empty sparse set&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;&lt;code&gt;N&lt;/code&gt; is zero, &lt;code&gt;dense&lt;/code&gt; and &lt;code&gt;sparse&lt;/code&gt; are allocated but not initialised.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nc&quot;&gt;rho_ss&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;rho_ss_alloc&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;rho_ss_id&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;max_id&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;max_id&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;max_id&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;rho_ss_id_max&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-kt&quot;&gt;size_t&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;array_size&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;rho_ss_id&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;max_id&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nc&quot;&gt;rho_ss&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt;         &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;dense&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;malloc&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;array_size&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;sparse&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;malloc&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;array_size&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;dense&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;||&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;sparse&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;free&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;dense&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;free&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;sparse&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;dense&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;sparse&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;max_id&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;adding-a-first-item&quot;&gt;Adding a first item&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Let&amp;#8217;s add &lt;code&gt;4&lt;/code&gt; as a first item to the set.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;imageblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;img src=&quot;/images/sparse-sets/sparse-set-add-first-item.png&quot; alt=&quot;Sparse set with one item added&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;First, we add the item to the &lt;code&gt;dense&lt;/code&gt; at index &lt;code&gt;0&lt;/code&gt; (this is the current value of &lt;code&gt;N&lt;/code&gt;).
Then we write &lt;code&gt;N&lt;/code&gt; to the &lt;code&gt;sparse&lt;/code&gt; array at index &lt;code&gt;4&lt;/code&gt;.
Now both slots in the &lt;code&gt;dense&lt;/code&gt; and &lt;code&gt;sparse&lt;/code&gt; arrays point to each other.
Lastly, we increase &lt;code&gt;N&lt;/code&gt; by one.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;adding-a-second-item&quot;&gt;Adding a second item&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Let&amp;#8217;s add &lt;code&gt;6&lt;/code&gt; as a second item.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;imageblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;img src=&quot;/images/sparse-sets/sparse-set-add-second-item.png&quot; alt=&quot;Sparse set with two items added&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;The steps are the same.
We put &lt;code&gt;6&lt;/code&gt; to the next free slot of the &lt;code&gt;dense&lt;/code&gt; array, write &lt;code&gt;N&lt;/code&gt; to the &lt;code&gt;sparse&lt;/code&gt; at index &lt;code&gt;6&lt;/code&gt;, and increase &lt;code&gt;N&lt;/code&gt; by one.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;The &lt;code&gt;sparse&lt;/code&gt; array has holes, while the &lt;code&gt;dense&lt;/code&gt; array items are placed next to each other.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;adding-a-third-item&quot;&gt;Adding a third item&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Now we add &lt;code&gt;0&lt;/code&gt; as a third item.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;imageblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;img src=&quot;/images/sparse-sets/sparse-set-add-third-item.png&quot; alt=&quot;Sparse set with three items added&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;There is nothing new here.
The item we added is &lt;code&gt;0&lt;/code&gt; so we put its &lt;code&gt;dense&lt;/code&gt; index (which is &lt;code&gt;2&lt;/code&gt;) to &lt;code&gt;sparse&lt;/code&gt; at index &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;And this is how we can implement it:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nf&quot;&gt;rho_ss_add&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nc&quot;&gt;rho_ss&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;rho_ss_id&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;rho_ss_has&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;dense&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;sparse&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;The &lt;code&gt;rho_ss_has&lt;/code&gt; function checks if the given item is in the sparse set.
We implement it in the next section.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;checking-whether-an-item-is-in-the-set&quot;&gt;Checking whether an item is in the set&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Let&amp;#8217;s test if &lt;code&gt;6&lt;/code&gt; is in the set.
To do so we go to the &lt;code&gt;sparse&lt;/code&gt; array and check its value at index &lt;code&gt;6&lt;/code&gt;.
The value is &lt;code&gt;1&lt;/code&gt;.
Now we use this value as an index in the &lt;code&gt;dense&lt;/code&gt; array.
The &lt;code&gt;dense&lt;/code&gt; has value &lt;code&gt;6&lt;/code&gt; at this index, which means &apos;yes, the set contains item six&apos;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Now let&amp;#8217;s check if &lt;code&gt;9&lt;/code&gt; is in the set.
We go to the &lt;code&gt;sparse&lt;/code&gt; array and check its value at index &lt;code&gt;9&lt;/code&gt;.
The value is garbage (we haven&amp;#8217;t initialised the memory, remember?) and can be anything, let&amp;#8217;s say it is &lt;code&gt;X&lt;/code&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;If &lt;code&gt;X &amp;gt;= N&lt;/code&gt; then &lt;code&gt;X&lt;/code&gt; is out of bounds index for the &lt;code&gt;dense&lt;/code&gt; array and &lt;code&gt;9&lt;/code&gt; is not in the set.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Suppose &lt;code&gt;X&lt;/code&gt; is less than &lt;code&gt;N&lt;/code&gt;.
In our case it should be either of &lt;code&gt;0&lt;/code&gt;, &lt;code&gt;1&lt;/code&gt;, or &lt;code&gt;2&lt;/code&gt; because &lt;code&gt;N&lt;/code&gt; is &lt;code&gt;3&lt;/code&gt;.
We check if the &lt;code&gt;dense&lt;/code&gt; array&amp;#8217;s value at index &lt;code&gt;X&lt;/code&gt; is &lt;code&gt;9&lt;/code&gt;.
It is not and it cannot be, so the answer is &apos;no, the set does not contain item nine&apos;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Now we can implement the operation:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-kt&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nf&quot;&gt;rho_ss_has&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nc&quot;&gt;rho_ss&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;rho_ss_id&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;rho_ss_id&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;dense_index&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;sparse&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;];&lt;/span&gt;

&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;dense_index&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;dense&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;dense_index&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;removing-an-item&quot;&gt;Removing an item&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Let&amp;#8217;s remove &lt;code&gt;6&lt;/code&gt; from the set.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;First, we replace &lt;code&gt;6&lt;/code&gt; with the last item from the &lt;code&gt;dense&lt;/code&gt; array, which is &lt;code&gt;0&lt;/code&gt;.
Then we have to update the &lt;code&gt;spare&lt;/code&gt; array at index &lt;code&gt;0&lt;/code&gt; with the new index of &lt;code&gt;0&lt;/code&gt; in the &lt;code&gt;dense&lt;/code&gt; array, which is &lt;code&gt;1&lt;/code&gt;.
And then reduce &lt;code&gt;N&lt;/code&gt; by one.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;imageblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;img src=&quot;/images/sparse-sets/sparse-set-remove-item.png&quot; alt=&quot;Sparse set with one item removed&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;In other words to remove an item from the set, we replace it with the last item in the &lt;code&gt;dense&lt;/code&gt; array and update the corresponding &lt;code&gt;sparse&lt;/code&gt; array&amp;#8217;s slot to point to the new location.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;The implementation is as follows:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nf&quot;&gt;rho_ss_remove&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nc&quot;&gt;rho_ss&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;rho_ss_id&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;rho_ss_has&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;rho_ss_id&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;dense_index&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;sparse&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;rho_ss_id&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt;         &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;dense&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;dense&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;dense_index&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;sparse&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt;       &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;dense_index&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;iterating-over-the-set&quot;&gt;Iterating over the set&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;This one is super simple.
You just iterate over the &lt;code&gt;dense&lt;/code&gt; array.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Like this (highlighted lines):&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nc&quot;&gt;rho_ss&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;rho_ss_alloc&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;tok-n&quot;&gt;rho_ss_add&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;tok-n&quot;&gt;rho_ss_add&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;tok-n&quot;&gt;rho_ss_add&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;tok-k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;rho_ss_id&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;%d &amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;dense&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;%s&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;tok-n&quot;&gt;rho_ss_free&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;clearing-the-set&quot;&gt;Clearing the set&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Just set &lt;code&gt;N&lt;/code&gt; to zero:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nf&quot;&gt;rho_ss_clear&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nc&quot;&gt;rho_ss&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ss&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Like I said before, most of the sparse set operations are super fast.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;whats-next&quot;&gt;What&amp;#8217;s next?&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;You could play with my &lt;a href=&quot;https://gitlab.com/manenko/rho-sparse-set/-/blob/master/include/rho/sparse_set.h&quot;&gt;implementation&lt;/a&gt; of the sparse set or (and this is preffered way) implement it on your own.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;</description>
        <pubDate>Sun, 23 May 2021 00:00:00 +0000</pubDate>
        <link>https://manenko.com/2021/05/23/sparse-sets.html</link>
        <guid isPermaLink="true">https://manenko.com/2021/05/23/sparse-sets.html</guid>
        
        <category>programming</category>
        
        <category>C</category>
        
        <category>algorithm</category>
        
        <category>data structure</category>
        
        
      </item>
    
      <item>
        <title>How to make Unreal Engine work correctly with CLion?</title>
        <description>&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;&lt;a href=&quot;https://www.unrealengine.com/&quot;&gt;Unreal Engine&lt;/a&gt; by Epic Games is a masterpiece.
THE Game Engine.
It is super powerful and convenient to use.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;There is a problem, though.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Epic Games developers use Windows and Visual Studio to develop the engine and don&amp;#8217;t invest the same amount of time to other development tools.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Unreal Engine supports the following source code editors:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;ulist&quot;&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Xcode&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Visual Studio&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Visual Studio Code&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;CLion&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Visual Studio and Visual Studio Code are the first-class citizens and work well with UE4.
More importantly, Visual Studio Code is cross-platform so you can just use that.
It is okay text editor.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Xcode and CLion on the other hand are next to impossible to use with the project files generated by UE4.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;JetBrains are good in making IDEs and CLion is a perfect C++ IDE.
THE perfect engine needs THE perfect IDE so let&amp;#8217;s marry them.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The problem&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;The current version of UE4 (4.26) generates incorrect CMake project files.
The bug exists for a long time but has not been fixed yet in any official build of the engine.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;The problem with the files it generates is that they don&amp;#8217;t have a few important project-wide macro definitions.
For example, each project has a &lt;code&gt;${PROJECT_NAME}_API&lt;/code&gt; macro.
The macro is used for exporting/importing C++ functions and classes, which is required on Windows, where it expands to &lt;code&gt;__declspec(dllimport)&lt;/code&gt; or &lt;code&gt;__declspec(dllexport)&lt;/code&gt; storage-class attributes.
The symbol is not defined which makes C++ definitions incorrect and CLion yells at you about syntatic errors in the code. This also breaks autocomplete.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Other examples are &lt;code&gt;GENERATED_BODY&lt;/code&gt;, &lt;code&gt;UCLASS&lt;/code&gt;, &lt;code&gt;UPROPERTY&lt;/code&gt; and many more U4 macros which are not properly defined for CMake-based projects because some of the symbols they depend on are not defined either.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;the-solution&quot;&gt;The solution&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;If you search the Internet hard enough, you can find a patch that fixes the problem.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;So go and grab the &lt;a href=&quot;https://gist.github.com/ericwomer/142650e65473087073f30e5fb97fd6e8&quot;&gt;patch&lt;/a&gt;.
Then open your terminal, go to the engine source code (for macOS it should be &lt;code&gt;/Users/Shared/Epic Games/UE_4.26&lt;/code&gt;).
Then apply the patch:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code&gt;&lt;span&gt;&lt;/span&gt;$ patch -p1 &amp;lt; /path/to/ue4_25-defines-fix.patch&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Now, if you use &lt;code&gt;zsh&lt;/code&gt; as I do, switch to &lt;code&gt;bash&lt;/code&gt; temporarely and source a setup environment script for your platform, in my case macOS:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code&gt;&lt;span&gt;&lt;/span&gt;$ bash
bash-3.2$ source Engine/Build/BatchFiles/Mac/SetupEnvironment.sh -mono Engine/Build/BatchFiles/Mac&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;The next step is to build and install Unreal Build Tool:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code&gt;&lt;span&gt;&lt;/span&gt;bash-3.2$ xbuild Engine/Source/Programs/UnrealBuildTool/UnrealBuildTool.csproj&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;The command will spit a deprecation warning on you, then compile the tool and install it to the right place replacing the old one so that the Unreal Editor can pick it up.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Now you can generate CLion project and open the project in the IDE.
It will take some time for it to load all symbols, though.
Select &lt;code&gt;${PROJECT_NAME}Editor|Debug&lt;/code&gt; build target and build it each time you want to make your code changes available to the Unreal Editor.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;imageblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;img src=&quot;/images/how-to-make-unreal-engine-work-correctly-with-clion/clion-auto-complete.png&quot; alt=&quot;CLion: Autocomplete&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;what-about-other-ides-and-editors-that-support-cmake&quot;&gt;What about other IDEs and editors that support CMake?&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;In this post I described how to generate correct CMake files.
Once you have the files, you can import them into any IDE/editor that supports CMake and everything should work out of the box.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;For example, here I have a project opened in Emacs:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;imageblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;img src=&quot;/images/how-to-make-unreal-engine-work-correctly-with-clion/emacs-auto-complete.png&quot; alt=&quot;Emacs: Autocomplete&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Symbol navigation, autocomplete, documentation browser, and refactor work perfectly well.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Happy coding! :-)&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;</description>
        <pubDate>Tue, 18 May 2021 00:00:00 +0000</pubDate>
        <link>https://manenko.com/2021/05/18/how-to-make-unreal-engine-work-correctly-with-clion.html</link>
        <guid isPermaLink="true">https://manenko.com/2021/05/18/how-to-make-unreal-engine-work-correctly-with-clion.html</guid>
        
        <category>programming</category>
        
        <category>CMake</category>
        
        <category>C++</category>
        
        <category>C</category>
        
        <category>build-systems</category>
        
        <category>UE4</category>
        
        <category>unreal</category>
        
        <category>macOS</category>
        
        
      </item>
    
      <item>
        <title>Managing external dependencies with CMake</title>
        <description>&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;&lt;a href=&quot;http://cmake.org/&quot;&gt;CMake&lt;/a&gt; is a de facto standard build tool for
C++. There are those who love it, and there are those who hate it. The
tool had a few problems in the past, but
&lt;a href=&quot;https://gist.github.com/mbinna/c61dbb39bca0e4fb7d1f73b0d66a4fd1&quot;&gt;Modern
CMake&lt;/a&gt; solved most of them and continued to evolve and add more and
more features.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Watch &lt;a href=&quot;https://www.youtube.com/watch?v=bsXLMQ6WgIk&quot;&gt;Effective CMake&lt;/a&gt;
talk by Daniel Pfeifer and
&lt;a href=&quot;https://www.youtube.com/watch?v=eC9-iRN2b04&quot;&gt;Using Modern CMake
Patterns to Enforce a Good Modular Design&lt;/a&gt; by Mathieu Ropert to get an
idea and hints on modern CMake.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;In this blog post, I describe the approach I use to manage external
dependencies in C++ projects.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;The dependencies have to have a valid
CMake configuration, i.e. they have to export their targets as
described in Daniel Pfeifer&amp;#8217;s &quot;Effective CMake&quot; starting at
&lt;a href=&quot;https://youtu.be/bsXLMQ6WgIk?t=2254&quot;&gt;37:34&lt;/a&gt;, Henry Schreiner&amp;#8217;s
&lt;a href=&quot;https://cliutils.gitlab.io/modern-cmake/chapters/install.html&quot;&gt;Modern
CMake: Exporting and Installing&lt;/a&gt;, and
&lt;a href=&quot;https://pabloariasal.github.io/2018/02/19/its-time-to-do-cmake-right&quot;&gt;It&amp;#8217;s
Time To Do CMake Right&lt;/a&gt; by Pablo Arias.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;typical-project-layout&quot;&gt;Typical project layout&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Let&amp;#8217;s review the layout of one of my C++ projects:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;sh&quot;&gt;&lt;span&gt;&lt;/span&gt;$&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;outpost-cpp&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;git:&lt;span class=&quot;tok-o&quot;&gt;(&lt;/span&gt;master&lt;span class=&quot;tok-o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;tree&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;-L&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-m&quot;&gt;2&lt;/span&gt;
.
├──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;build
├──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;cmake
│&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;├──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;Catch.cmake
│&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;├──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;CatchAddTests.cmake
│&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;└──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;ParseAndAddCatchTests.cmake
├──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;conker
│&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;├──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;include
│&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;├──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;src
│&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;└──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;CMakeLists.txt
├──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;extern
│&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;└──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;CMakeLists.txt
├──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;outpost
│&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;├──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;assets
│&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;├──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;include
│&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;├──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;src
│&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;└──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;CMakeLists.txt
├──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;test&lt;/span&gt;
│&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;├──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;src
│&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;└──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;CMakeLists.txt
├──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;CMakeLists.txt
└──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;Doxyfile

&lt;span class=&quot;tok-m&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;directories,&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-m&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;files&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;There are a few items in the root directory of the project:&lt;/p&gt;
&lt;/div&gt;
&lt;table class=&quot;tableblock frame-all grid-all stretch&quot;&gt;
&lt;colgroup&gt;
&lt;col style=&quot;width: 10%;&quot;&gt;
&lt;col style=&quot;width: 90%;&quot;&gt;
&lt;/colgroup&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th class=&quot;tableblock halign-left valign-top&quot;&gt;Directory&lt;/th&gt;
&lt;th class=&quot;tableblock halign-left valign-top&quot;&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td class=&quot;tableblock halign-left valign-top&quot;&gt;&lt;p class=&quot;tableblock&quot;&gt;&lt;strong&gt;.&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;
&lt;td class=&quot;tableblock halign-left valign-top&quot;&gt;&lt;p class=&quot;tableblock&quot;&gt;Project root with the root &lt;code&gt;CMakeLists.txt&lt;/code&gt;&lt;/p&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;tableblock halign-left valign-top&quot;&gt;&lt;p class=&quot;tableblock&quot;&gt;&lt;strong&gt;build&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;
&lt;td class=&quot;tableblock halign-left valign-top&quot;&gt;&lt;p class=&quot;tableblock&quot;&gt;That&amp;#8217;s where you do &lt;code&gt;cd build &amp;amp;&amp;amp; cmake .. &amp;amp;&amp;amp; make&lt;/code&gt; to build the project&lt;/p&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;tableblock halign-left valign-top&quot;&gt;&lt;p class=&quot;tableblock&quot;&gt;&lt;strong&gt;cmake&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;
&lt;td class=&quot;tableblock halign-left valign-top&quot;&gt;&lt;p class=&quot;tableblock&quot;&gt;Consists of custom CMake modules&lt;/p&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;tableblock halign-left valign-top&quot;&gt;&lt;p class=&quot;tableblock&quot;&gt;&lt;strong&gt;conker&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;
&lt;td class=&quot;tableblock halign-left valign-top&quot;&gt;&lt;p class=&quot;tableblock&quot;&gt;A subproject for a library&lt;/p&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;tableblock halign-left valign-top&quot;&gt;&lt;p class=&quot;tableblock&quot;&gt;&lt;strong&gt;extern&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;
&lt;td class=&quot;tableblock halign-left valign-top&quot;&gt;&lt;p class=&quot;tableblock&quot;&gt;External dependencies&lt;/p&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;tableblock halign-left valign-top&quot;&gt;&lt;p class=&quot;tableblock&quot;&gt;&lt;strong&gt;outpost&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;
&lt;td class=&quot;tableblock halign-left valign-top&quot;&gt;&lt;p class=&quot;tableblock&quot;&gt;A subproject for a main application&lt;/p&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class=&quot;tableblock halign-left valign-top&quot;&gt;&lt;p class=&quot;tableblock&quot;&gt;&lt;strong&gt;test&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;
&lt;td class=&quot;tableblock halign-left valign-top&quot;&gt;&lt;p class=&quot;tableblock&quot;&gt;A subproject for unit-tests&lt;/p&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;describing-dependencies&quot;&gt;Describing dependencies&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;The &lt;code&gt;extern/CMakeLists.txt&lt;/code&gt; file is a central repository for the
dependencies. Make it available from the root &lt;code&gt;CMakeLists.txt&lt;/code&gt;:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;title&quot;&gt;CMakeLists.txt&lt;/div&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;cmake&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;add_subdirectory&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;extern&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Let&amp;#8217;s take a closer look at the dependencies file:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;title&quot;&gt;extern/CMakeLists.txt&lt;/div&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;cmake&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;cmake_minimum_required&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;VERSION&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;3.11&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;FATAL_ERROR&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-nb&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;FetchContent&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-c&quot;&gt;# ------------------------------------------------------------------------------&lt;/span&gt;
&lt;span class=&quot;tok-c&quot;&gt;# A modern, C++-native, header-only, test framework for unit-tests,&lt;/span&gt;
&lt;span class=&quot;tok-c&quot;&gt;# TDD and BDD - using C++11, C++14, C++17 and later&lt;/span&gt;
&lt;span class=&quot;tok-nb&quot;&gt;FetchContent_Declare&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;extern_catch2&lt;/span&gt;

&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;GIT_REPOSITORY&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;https://github.com/catchorg/Catch2.git&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;GIT_TAG&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;v2.2.3&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-nb&quot;&gt;FetchContent_GetProperties&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;extern_catch2&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-nb&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;NOT&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;extern_catch2_POPULATED&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;FetchContent_Populate&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;extern_catch2&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;add_subdirectory&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;extern_catch2_SOURCE_DIR&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;extern_catch2_BINARY_DIR&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;EXCLUDE_FROM_ALL&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-nb&quot;&gt;endif&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;tok-c&quot;&gt;# ------------------------------------------------------------------------------&lt;/span&gt;
&lt;span class=&quot;tok-c&quot;&gt;# A multi-platform library for OpenGL, OpenGL ES, Vulkan, window and&lt;/span&gt;
&lt;span class=&quot;tok-c&quot;&gt;# input http://www.glfw.org/&lt;/span&gt;
&lt;span class=&quot;tok-nb&quot;&gt;FetchContent_Declare&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;extern_glfw&lt;/span&gt;

&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;GIT_REPOSITORY&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;https://github.com/glfw/glfw.git&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-nb&quot;&gt;FetchContent_GetProperties&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;extern_glfw&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-nb&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;NOT&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;extern_glfw_POPULATED&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;FetchContent_Populate&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;extern_glfw&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;add_subdirectory&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;extern_glfw_SOURCE_DIR&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;extern_glfw_BINARY_DIR&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;EXCLUDE_FROM_ALL&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-nb&quot;&gt;endif&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;tok-c&quot;&gt;# ------------------------------------------------------------------------------&lt;/span&gt;
&lt;span class=&quot;tok-c&quot;&gt;# https://devblogs.nvidia.com/linking-opengl-server-side-rendering/&lt;/span&gt;
&lt;span class=&quot;tok-nb&quot;&gt;find_package&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;OpenGL&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt;     &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;REQUIRED&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;COMPONENTS&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;OpenGL&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;The script uses CMake&amp;#8217;s
&lt;a href=&quot;https://cmake.org/cmake/help/git-stage/module/FetchContent.html&quot;&gt;FetchContent&lt;/a&gt;
module which enables populating content at configure time. That&amp;#8217;s how you
describe a dependency:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;title&quot;&gt;extern/CMakeLists.txt&lt;/div&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;cmake&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;FetchContent_Declare&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;extern_catch2&lt;/span&gt;

&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;GIT_REPOSITORY&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;https://github.com/catchorg/Catch2.git&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;GIT_TAG&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;v2.2.3&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-nb&quot;&gt;FetchContent_GetProperties&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;extern_catch2&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-nb&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;NOT&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;extern_catch2_POPULATED&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;FetchContent_Populate&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;extern_catch2&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;add_subdirectory&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;extern_catch2_SOURCE_DIR&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;extern_catch2_BINARY_DIR&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;EXCLUDE_FROM_ALL&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-nb&quot;&gt;endif&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;()&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;In other words, you declare where and how to get the dependency first
and then check if it is already available. If it is not avialable yet,
then you fetch it and include into the build using the &lt;code&gt;add_subdirectory&lt;/code&gt;
command.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;importing-dependencies-in-subprojects&quot;&gt;Importing dependencies in subprojects&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;&lt;code&gt;FetchContent&lt;/code&gt; makes exported targets available to the build so that you can use
them in &lt;code&gt;target_link_libraries&lt;/code&gt;:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;title&quot;&gt;outpost/CMakeLists.txt&lt;/div&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;cmake&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;target_link_libraries&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;outpost&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;conker&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;glfw&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Here, I added the &lt;code&gt;glfw&lt;/code&gt; as a public dependency to the &lt;code&gt;outpost&lt;/code&gt; target.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Another example:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;title&quot;&gt;test/CMakeLists.txt&lt;/div&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;cmake&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;target_link_libraries&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;conker_tests&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;PRIVATE&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;Catch&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;conker&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;hierarchical-dependency-management&quot;&gt;Hierarchical dependency management&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;There are few ways to organise dependency management.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;sect2&quot;&gt;
&lt;h3 id=&quot;keep-all-dependencies-in-the-same-file&quot;&gt;Keep all dependencies in the same file&lt;/h3&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;It&amp;#8217;s good enough for simple projects that live in the same repository,
and there are no plans to move subprojects outside of it. That&amp;#8217;s what
I described in the previous sections.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;sh&quot;&gt;&lt;span&gt;&lt;/span&gt;├──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;CMakeLists.txt
├──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;extern
│&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;└──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;CMakeLists.txt
├──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;subproject_a
│&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;└──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;CMakeLists.txt
├──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;subproject_b
│&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;└──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;CMakeLists.txt
└──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;subproject_c
&lt;span class=&quot;tok-w&quot;&gt;      &lt;/span&gt;└──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;CMakeLists.txt&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect2&quot;&gt;
&lt;h3 id=&quot;each-subproject-describes-its-dependencies&quot;&gt;Each subproject describes its dependencies&lt;/h3&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;In this case, you make subprojects independent by adding their own
&lt;code&gt;external/CMakeLists.txt&lt;/code&gt;. Then you can extract any of them from the
source tree and build it without any further configuration if needed.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;sh&quot;&gt;&lt;span&gt;&lt;/span&gt;├──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;CMakeLists.txt
├──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;subproject_a
│&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;├──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;extern
│&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;│&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;└──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;CMakeLists.txt
│&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;└──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;CMakeLists.txt
├──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;subproject_b
│&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;├──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;extern
│&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;│&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;└──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;CMakeLists.txt
│&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;└──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;CMakeLists.txt
└──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;subproject_c
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;├──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;extern
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;│&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;└──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;CMakeLists.txt
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;└──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;CMakeLists.txt&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect2&quot;&gt;
&lt;h3 id=&quot;combination-of-the-previous-methods&quot;&gt;Combination of the previous methods&lt;/h3&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;You declare dependencies for each subproject in their own
&lt;code&gt;external/CMakeLists.txt&lt;/code&gt;. Then you declare (i.e. duplicate)
dependencies on one level higher than the subprojects. This is how you
override population of content specified anywhere lower in the project
hierarchy.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;sh&quot;&gt;&lt;span&gt;&lt;/span&gt;├──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;CMakeLists.txt
├──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;extern
│&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;└──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;CMakeLists.txt
├──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;subproject_a
│&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;├──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;extern
│&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;│&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;└──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;CMakeLists.txt
│&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;└──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;CMakeLists.txt
├──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;subproject_b
│&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;├──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;extern
│&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;│&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;└──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;CMakeLists.txt
│&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;└──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;CMakeLists.txt
└──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;subproject_c
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;├──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;extern
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;│&lt;span class=&quot;tok-w&quot;&gt;   &lt;/span&gt;└──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;CMakeLists.txt
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;└──&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;CMakeLists.txt&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect2&quot;&gt;
&lt;h3 id=&quot;the-end&quot;&gt;The end&lt;/h3&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;I hope this helps.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;</description>
        <pubDate>Sun, 30 Sep 2018 00:00:00 +0000</pubDate>
        <link>https://manenko.com/2018/09/30/manage-external-dependencies-with-cmake.html</link>
        <guid isPermaLink="true">https://manenko.com/2018/09/30/manage-external-dependencies-with-cmake.html</guid>
        
        <category>programming</category>
        
        <category>CMake</category>
        
        <category>C++</category>
        
        <category>C</category>
        
        <category>build-systems</category>
        
        
      </item>
    
      <item>
        <title>How to setup Emacs for Rust development</title>
        <description>&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;In this post I will show you how to setup Emacs for the Rust development. We will
start with empty setup and then move iteratively adding new features. At the end
of the process you should have something similar to the following screenshot:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;imageblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;img src=&quot;/images/setup-emacs-for-rust-development/emacs-rust-code-autocomplete.png&quot; alt=&quot;Emacs: Rust code autocomplete&quot; width=&quot;618&quot; height=&quot;558&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;initial-setup&quot;&gt;Initial setup&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Emacs loads it&amp;#8217;s configuration from &lt;em&gt;initialization file&lt;/em&gt; which can be
&lt;code&gt;~/.emacs&lt;/code&gt;, &lt;code&gt;~/.emacs.el&lt;/code&gt;, or &lt;code&gt;.emacs.d/init.el&lt;/code&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;We will use &lt;code&gt;.emacs.d/init.el&lt;/code&gt; file as configuration script. Let&amp;#8217;s create it:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;sh&quot;&gt;&lt;span&gt;&lt;/span&gt;$&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;mkdir&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;~/.emacs.d
$&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;cd&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;~/.emacs.d
$&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;touch&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;init.el
$&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;git&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;init&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;.&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Here, we created initialization file as well as new git repository inside
&lt;code&gt;.emacs.d&lt;/code&gt; directory. Git step is &lt;strong&gt;optional&lt;/strong&gt; and it has nothing to do with
Emacs configuration. However, I strictly advise not to skip it, because putting
your configuration directory under the source control allows you to have version
tracking, rollbacks, backups (just create a new repository on
&lt;a href=&quot;http://bitbucket.com&quot; class=&quot;bare&quot;&gt;http://bitbucket.com&lt;/a&gt; or similar resource and sync your local repository with the
remote one).&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Now, create &lt;code&gt;~/.emacs.d/.gitignore&lt;/code&gt; file with the following content:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;title&quot;&gt;~/.emacs.d/.gitignore&lt;/div&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre&gt;*~
places
\.recentf
\.smex-items
ido.last
projectile-bookmarks.eld
projectile.cache
auto-save-list
backups
elpa/
eshell&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Commit your changes to the source control.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;what-to-put-into-init-el-file&quot;&gt;What to put into init.el file&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;You can put all your configuration inside Emacs initialization file.
However this is not a best idea. As we know from software development it is much
better to group similar things into modules/packages/you-name-it to simplify
future reuse, changes, etc.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;We will use &lt;code&gt;init.el&lt;/code&gt; file as an entry point that loads all other scripts.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Open &lt;code&gt;init.el&lt;/code&gt; file and add the following code to it:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;title&quot;&gt;~/.emacs.d/init.el&lt;/div&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;lisp&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;add-to-list&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;load-path&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;expand-file-name&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;lisp&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;user-emacs-directory&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;provide&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;init&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Here we tell Emacs that we will put all our custom scripts into &lt;code&gt;~/.emacs.d/lisp&lt;/code&gt;
folder so that Emacs can find them when we use &lt;code&gt;require&lt;/code&gt; function to load these
scripts. You can read more about loading scripts in Emacs Lisp
&lt;a href=&quot;https://www.emacswiki.org/emacs/LoadingLispFiles&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;packages&quot;&gt;Packages&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Package is an Emacs Lisp program that provides additional features. You can read
about Emacs packages
&lt;a href=&quot;https://www.gnu.org/software/emacs/manual/html_node/emacs/Packages.html&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;In fact all our scripts will be Emacs packages because this simplifies
dependency management and has ultra-speed load time.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Add &lt;code&gt;(require &apos;init-elpa)&lt;/code&gt; line to the &lt;code&gt;~/.emacs.d/init.el&lt;/code&gt; script. Now it
should look like this:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;title&quot;&gt;~/.emacs.d/init.el&lt;/div&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;lisp&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;add-to-list&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;load-path&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;expand-file-name&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;lisp&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;user-emacs-directory&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;init-elpa&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;provide&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;init&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;This line will load &lt;code&gt;~/.emacs.d/lisp/init-elpa.el&lt;/code&gt; and all its dependencies.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Now create &lt;code&gt;~/.emacs.d/lisp/init-elpa.el&lt;/code&gt; file, open it for editing and put the
following code in it:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;title&quot;&gt;~/.emacs.d/lisp/init-elpa.el&lt;/div&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;lisp&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;package&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;defun&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;require-package&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nc&quot;&gt;package&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;Install given PACKAGE if it was not installed before.&amp;quot;&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;package-installed-p&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nc&quot;&gt;package&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;tok-no&quot;&gt;t&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;progn&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;unless&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;assoc&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nc&quot;&gt;package&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;package-archive-contents&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;	&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;package-refresh-contents&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;package-install&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nc&quot;&gt;package&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;))))&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;add-to-list&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;package-archives&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;	     &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;melpa&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;https://melpa.org/packages/&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;package-initialize&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;provide&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;init-elpa&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;This package initializes Emacs package system, adds &lt;a href=&quot;http://melpa.org/&quot;&gt;MELPA&lt;/a&gt; the
list of available package archives and defines a function &lt;code&gt;require-package&lt;/code&gt; that
installs the given package if it was not installed before.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;exec-path-os-x-only&quot;&gt;Exec path [OS X only]&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;The next step is specific to OS X, where an Emacs instance started from the GUI
will have a different environment than a shell in a terminal window,
because OS X does not run a shell during the login. Obviously this will lead to
unexpected results when calling external utilities like &lt;code&gt;make&lt;/code&gt; from Emacs.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;So here we are going to solve this issue by using &lt;code&gt;exec-path-from-shell&lt;/code&gt; package.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Add the line &lt;code&gt;(require &apos;init-exec-path)&lt;/code&gt; after the last &lt;code&gt;require&lt;/code&gt; in the
&lt;code&gt;~/.emacs.d/init.el&lt;/code&gt; file:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;title&quot;&gt;~/.emacs.d/init.el&lt;/div&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;lisp&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;add-to-list&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;load-path&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;expand-file-name&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;lisp&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;user-emacs-directory&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;init-elpa&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;init-exec-path&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;provide&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;init&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;admonitionblock note&quot;&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;td class=&quot;icon&quot;&gt;
&lt;i class=&quot;fa icon-note&quot; title=&quot;Note&quot;&gt;&lt;/i&gt;
&lt;/td&gt;
&lt;td class=&quot;content&quot;&gt;
In the future, when I say &lt;em&gt;add require to the initialization file&lt;/em&gt; it means
add it to the &lt;code&gt;~/.emacs.d/init.el&lt;/code&gt; file after the last &lt;code&gt;require&lt;/code&gt; as in previous
example.
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Now open &lt;code&gt;~/.emacs.d/lisp/init-exec-path.el&lt;/code&gt; and fill it with the following code:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;title&quot;&gt;~/.emacs.d/lisp/init-exec-path.el&lt;/div&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;lisp&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;init-elpa&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;require-package&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;exec-path-from-shell&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;when&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;memq&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;window-system&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;mac&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;ns&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;exec-path-from-shell-initialize&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;provide&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;init-exec-path&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;First, we &lt;code&gt;require&lt;/code&gt; &lt;code&gt;&apos;init-elpa&lt;/code&gt; package we created before to use its &lt;code&gt;require-package&lt;/code&gt;
function.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Second, we execute &lt;code&gt;exec-path-from-shell-initialize&lt;/code&gt; function only
if this instance of Emacs is run under the OS X.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;ui&quot;&gt;UI&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Now it&amp;#8217;s time to deal with default Emacs UI. Add &lt;code&gt;(require &apos;init-ui)&lt;/code&gt; to Emacs
initialization file, then open &lt;code&gt;~/.emacs.d/lisp/init-ui.el&lt;/code&gt; and add the following
lines:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;title&quot;&gt;~/.emacs.d/lisp/init-ui.el&lt;/div&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;lisp&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;init-elpa&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;require-package&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;atom-one-dark-theme&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;require-package&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;golden-ratio&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;golden-ratio&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;setq&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;inhibit-startup-message&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-no&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;menu-bar-mode&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-mi&quot;&gt;-1&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;when&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;fboundp&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;tool-bar-mode&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;tool-bar-mode&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-mi&quot;&gt;-1&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;when&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;fboundp&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;scroll-bar-mode&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;scroll-bar-mode&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-mi&quot;&gt;-1&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;set-face-attribute&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;default&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-no&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;:height&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-mi&quot;&gt;140&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;setq-default&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;line-spacing&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-mf&quot;&gt;0.4&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;setq&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;x-select-enable-clipboard&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-no&quot;&gt;t&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;x-select-enable-primary&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-no&quot;&gt;t&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;save-interprogram-paste-before-kill&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-no&quot;&gt;t&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;apropos-do-all&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-no&quot;&gt;t&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;mouse-yank-at-point&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-no&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;load-theme&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;atom-one-dark&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-no&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;blink-cursor-mode&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;setq-default&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;cursor-type&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;bar&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;set-cursor-color&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;#cccccc&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;setq&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;ring-bell-function&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;ignore&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;golden-ratio-mode&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;provide&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;init-ui&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;There is a little bit more code in this package than in the previous.
It turns off menu bar, scroll bar, and tool bar, as well as standard welcome
message. It installs and loads Atom One dark syntax theme.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Another important feature is &lt;a href=&quot;https://github.com/roman/golden-ratio.el&quot;&gt;Golden Ratio&lt;/a&gt;.
It resizes Emacs windows automatically to make the window that has the focus to
have perfect size for editing.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;editing&quot;&gt;Editing&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;The next step is to make editing a little bit more convenient.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Add &lt;code&gt;(require &apos;init-editing)&lt;/code&gt; to the initialization file, open
&lt;code&gt;~/.emacs.d/lisp/init-editing.el&lt;/code&gt; script and add the following:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;title&quot;&gt;~/.emacs.d/lisp/init-editing.el&lt;/div&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;lisp&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;init-elpa&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;saveplace&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;require-package&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;rainbow-delimiters&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;require-package&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;flycheck&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-c1&quot;&gt;;; Highlights matching parenthesis&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;show-paren-mode&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-c1&quot;&gt;;; Highlight current line&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;global-hl-line-mode&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-c1&quot;&gt;;; Interactive search key bindings. By default, C-s runs&lt;/span&gt;
&lt;span class=&quot;tok-c1&quot;&gt;;; isearch-forward, so this swaps the bindings.&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;global-set-key&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;kbd&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;C-s&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;isearch-forward-regexp&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;global-set-key&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;kbd&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;C-r&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;isearch-backward-regexp&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;global-set-key&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;kbd&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;C-M-s&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;isearch-forward&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;global-set-key&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;kbd&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;C-M-r&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;isearch-backward&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;define-key&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;global-map&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;kbd&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;RET&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;newline-and-indent&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;add-hook&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;after-init-hook&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nf&quot;&gt;#&amp;#39;&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;global-flycheck-mode&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-c1&quot;&gt;;; When you visit a file, point goes to the last place where it&lt;/span&gt;
&lt;span class=&quot;tok-c1&quot;&gt;;; was when you previously visited the same file.&lt;/span&gt;
&lt;span class=&quot;tok-c1&quot;&gt;;; http://www.emacswiki.org/emacs/SavePlace&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;setq-default&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;save-place&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-no&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-c1&quot;&gt;;; keep track of saved places in ~/.emacs.d/places&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;setq&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;save-place-file&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;concat&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;user-emacs-directory&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;places&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;tok-c1&quot;&gt;;; Emacs can automatically create backup files. This tells Emacs to&lt;/span&gt;
&lt;span class=&quot;tok-c1&quot;&gt;;; put all backups in ~/.emacs.d/backups. More info:&lt;/span&gt;
&lt;span class=&quot;tok-c1&quot;&gt;;; http://www.gnu.org/software/emacs/manual/html_node/elisp/Backup-Files.html&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;setq&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;backup-directory-alist&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;.&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;concat&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;user-emacs-directory&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;                                               &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;backups&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;))))&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;setq&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;auto-save-default&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-no&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;defun&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;toggle-comment-on-line&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;Comment or uncomment current line.&amp;quot;&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;interactive&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;comment-or-uncomment-region&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;line-beginning-position&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;line-end-position&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)))&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;global-set-key&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;kbd&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;C-;&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;toggle-comment-on-line&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;add-hook&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;prog-mode-hook&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nf&quot;&gt;#&amp;#39;&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;rainbow-delimiters-mode&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;provide&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;init-editing&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;It is pretty good documented. Just go through it and read the source :)&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;navigation&quot;&gt;Navigation&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Add &lt;code&gt;(require &apos;init-navigation)&lt;/code&gt; to the initialization file and open
&lt;code&gt;~/.emacs.d/lisp/init-navigation.el&lt;/code&gt; script.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;We are going to install and configure the following packages:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;ulist&quot;&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&quot;https://www.emacswiki.org/emacs/InteractivelyDoThings&quot;&gt;Interactively Do Things&lt;/a&gt;
or &lt;code&gt;Ido&lt;/code&gt; mode. I advise you to read
&lt;a href=&quot;https://www.masteringemacs.org/article/introduction-to-ido-mode&quot;&gt;Introduction to Ido Mode&lt;/a&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&quot;https://github.com/DarwinAwardWinner/ido-ubiquitous&quot;&gt;ido-ubiquitous&lt;/a&gt; mode.
This package replaces stock emacs completion with Ido completion wherever it
is possible to do so without breaking things.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&quot;https://github.com/nonsequitur/smex&quot;&gt;Smex&lt;/a&gt;. Built on top of Ido, it provides a
convenient interface to your recently and most frequently used commands. And
to all the other commands, too.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&quot;https://www.emacswiki.org/emacs/RecentFiles&quot;&gt;Recentf&lt;/a&gt;. Recentf is a minor mode
that builds a list of recently opened files. This list is automatically
saved across sessions on exiting Emacs - you can then access this list through
a command or the menu.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&quot;https://github.com/bbatsov/projectile&quot;&gt;Projectile&lt;/a&gt;. It provides easy project
management and navigation. Please refer to the link I&amp;#8217;ve provide for complete
list of features.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;And here is the code:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;title&quot;&gt;~/.emacs.d/lisp/init-navigation.el&lt;/div&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;lisp&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;init-elpa&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;ido&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;recentf&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;require-package&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;ido-ubiquitous&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;require-package&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;smex&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;require-package&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;projectile&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;setq&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;recentf-save-file&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;concat&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;user-emacs-directory&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;.recentf&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;recentf-mode&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;setq&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;recentf-max-menu-items&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;ido-mode&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-no&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;setq&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;ido-enable-flex-matching&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-no&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;setq&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;ido-use-filename-at-point&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-no&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;setq&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;ido-auto-merge-work-directories-length&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-mi&quot;&gt;-1&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;setq&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;ido-use-virtual-buffers&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-no&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;ido-ubiquitous-mode&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-c1&quot;&gt;;; Shows a list of buffers&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;global-set-key&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;kbd&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;C-x C-b&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;ibuffer&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;setq&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;smex-save-file&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;concat&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;user-emacs-directory&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;.smex-items&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;smex-initialize&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;global-set-key&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;kbd&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;M-x&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;smex&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;projectile-global-mode&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-c1&quot;&gt;;; Enable move point from window to window using Shift and the arrow keys&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;windmove-default-keybindings&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;provide&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;init-navigation&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;miscellaneous&quot;&gt;Miscellaneous&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Here we will setup a few things to make Emacs to be more convenient to use.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Add &lt;code&gt;(require &apos;init-miscellaneous)&lt;/code&gt; to the initialization file and open the
&lt;code&gt;~/.emacs.d/lisp/init-miscellaneous.el&lt;/code&gt; for editing. Add the following lines:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;title&quot;&gt;~/.emacs.d/lisp/init-miscellaneous.el&lt;/div&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;lisp&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-c1&quot;&gt;;; Changes all yes/no questions to y/n type&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;fset&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;yes-or-no-p&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;y-or-n-p&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-c1&quot;&gt;;; No need for ~ files when editing&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;setq&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;create-lockfiles&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-no&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;provide&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;init-miscellaneous&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;It changes &lt;code&gt;yes/no&lt;/code&gt; questions to &lt;code&gt;y/n&lt;/code&gt; type so that you can just press &lt;code&gt;y&lt;/code&gt; or &lt;code&gt;n&lt;/code&gt;,
instead of typing &lt;code&gt;yes&lt;/code&gt; or &lt;code&gt;no&lt;/code&gt;. Also this packages disables generation of
&lt;a href=&quot;https://www.gnu.org/software/emacs/manual/html_node/emacs/Interlocking.html&quot;&gt;Emacs lock files&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;text-completion&quot;&gt;Text Completion&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Emacs has very good text completion framework called
&lt;a href=&quot;http://company-mode.github.io&quot;&gt;company-mode&lt;/a&gt;. Let&amp;#8217;s install and configure it.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Add &lt;code&gt;(require &apos;init-company-mode)&lt;/code&gt; to the initialization file as usual, then
open &lt;code&gt;~/.emacs.d/lisp/init-company-mode.el&lt;/code&gt; script and add these lines:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;title&quot;&gt;~/.emacs.d/lisp/init-company-mode.el&lt;/div&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;lisp&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;init-elpa&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;require-package&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;company&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;company&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;setq&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;company-tooltip-align-annotations&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-no&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;add-hook&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;prog-mode-hook&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;company-mode&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;provide&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;init-company-mode&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Here, we enable company mode for all programming modes making it default
autocompletion engine.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Now save everything and commit to the source control.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;rust-support&quot;&gt;Rust Support&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Please, make sure that you have Rust installed before continue.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Now here is the thing. We have to install third-party tools to make Rust
autocompletion work.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;sect2&quot;&gt;
&lt;h3 id=&quot;install-racer&quot;&gt;Install Racer&lt;/h3&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Please, follow the &lt;a href=&quot;https://github.com/phildawes/racer#installation&quot;&gt;Racer installation guideline&lt;/a&gt;
to install it on your machine.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect2&quot;&gt;
&lt;h3 id=&quot;install-rust-sources&quot;&gt;Install Rust sources&lt;/h3&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Clone the &lt;a href=&quot;https://github.com/rust-lang/rust&quot; class=&quot;bare&quot;&gt;https://github.com/rust-lang/rust&lt;/a&gt; repository.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect2&quot;&gt;
&lt;h3 id=&quot;configure-rust-support-in-emacs&quot;&gt;Configure Rust Support in Emacs&lt;/h3&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;As usual, add &lt;code&gt;(require &apos;init-rust)&lt;/code&gt; to the initialization file and add the
following code to the &lt;code&gt;~/.emacs.d/lisp/init-rust.el&lt;/code&gt; package:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;title&quot;&gt;~/.emacs.d/lisp/init-rust.el&lt;/div&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;lisp&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;init-elpa&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;require-package&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;company&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;require-package&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;racer&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;require-package&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;rust-mode&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;require-package&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;flycheck&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;require-package&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;flycheck-rust&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;company&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;racer&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;rust-mode&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;electric&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;eldoc&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;flycheck&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;flycheck-rust&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;add-to-list&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;auto-mode-alist&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;\\.rs\\&amp;#39;&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;rust-mode&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;add-hook&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;rust-mode-hook&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-nf&quot;&gt;#&amp;#39;&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;company-mode&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;add-hook&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;rust-mode-hook&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;tok-nf&quot;&gt;#&amp;#39;&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;racer-mode&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;add-hook&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;racer-mode-hook&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nf&quot;&gt;#&amp;#39;&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;eldoc-mode&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;add-hook&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;flycheck-mode-hook&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nf&quot;&gt;#&amp;#39;&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;flycheck-rust-setup&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;add-hook&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;rust-mode-hook&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;lambda&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;	     &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;setq&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;racer-cmd&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;concat&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;getenv&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;HOME&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;/.rust-dev/racer/target/release/racer&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;	     &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;setq&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;racer-rust-src-path&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;concat&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;getenv&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;HOME&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;/.rust-dev/rust/src&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;             &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;local-set-key&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;kbd&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s&quot;&gt;&amp;quot;TAB&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-nf&quot;&gt;#&amp;#39;&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;company-indent-or-complete-common&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;	     &lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;electric-pair-mode&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)))&lt;/span&gt;

&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;provide&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-ss&quot;&gt;&amp;#39;init-rust&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;This package installs the following dependencies:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;ulist&quot;&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&quot;http://www.flycheck.org/en/latest/&quot;&gt;Flycheck&lt;/a&gt;. This is a modern on-the-fly
syntax checking extension.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&quot;https://github.com/flycheck/flycheck-rust&quot;&gt;flycheck-rust&lt;/a&gt;. It adds Rust syntax
checking and linting to Flycheck.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&quot;https://github.com/rust-lang/rust-mode&quot;&gt;rust-mode&lt;/a&gt;. This adds Rust syntax
highlighting, etc.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&quot;https://github.com/racer-rust/emacs-racer&quot;&gt;racer&lt;/a&gt;. It allows Emacs to use
&lt;a href=&quot;https://github.com/phildawes/racer&quot;&gt;Racer&lt;/a&gt; for Rust code completion and
navigation.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Please, change the path to the racer executable (&lt;code&gt;racer-cmd&lt;/code&gt; variable) and the
path to the Rust sources (&lt;code&gt;racer-rust-src-path&lt;/code&gt; variable) according to your
local environment.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;For example, I downloaded &lt;code&gt;racer&lt;/code&gt; to the &lt;code&gt;~/.rust-dev/racer&lt;/code&gt; folder and Rust
sources to the &lt;code&gt;~/.rust-dev/rust&lt;/code&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Save everything, commit to the source control, start Emacs. Wait until it finishes
installing missing packages, then restart it.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Now you should have Rust syntax highlighting, autocomplete, and project
navigation in your beloved Emacs.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;You can find my Emacs config &lt;a href=&quot;https://gitlab.com/manenko/dotfiles&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;That&amp;#8217;s it.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;</description>
        <pubDate>Wed, 03 Aug 2016 00:00:00 +0000</pubDate>
        <link>https://manenko.com/2016/08/03/setup-emacs-for-rust-development.html</link>
        <guid isPermaLink="true">https://manenko.com/2016/08/03/setup-emacs-for-rust-development.html</guid>
        
        <category>Emacs</category>
        
        <category>programming</category>
        
        <category>Rust</category>
        
        <category>racer</category>
        
        <category>flycheck</category>
        
        
      </item>
    
      <item>
        <title>GDM doesn&apos;t load included files from .Xresources in Arch Linux</title>
        <description>&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;I installed Arch Linux along with GNOME 3 yesterday. I have a &lt;a href=&quot;https://gitlab.com/manenko/dotfiles&quot;&gt;git repository&lt;/a&gt;
to keep all my settings the same across all devices.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;This is how my &lt;code&gt;.Xresources&lt;/code&gt; looks like:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-cp&quot;&gt;#include&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-cpf&quot;&gt;&amp;quot;Xresources.d/xterm&amp;quot;&lt;/span&gt;
&lt;span class=&quot;tok-cp&quot;&gt;#include&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-cpf&quot;&gt;&amp;quot;Xresources.d/Xft&amp;quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;I use different files for each application settings and &lt;code&gt;#include&lt;/code&gt; them into the main &lt;code&gt;.Xresources&lt;/code&gt; file. That&amp;#8217;s works
just fine.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;However, it seems that GDM ignores all &lt;code&gt;#include&lt;/code&gt;-s in &lt;code&gt;.Xresources&lt;/code&gt; file. I didn&amp;#8217;t have such issues in Fedora, so I
assume this is Arch Linux specific thing.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;After some investigation I found that &lt;code&gt;/etc/gdm/Xsession&lt;/code&gt; has the following code:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;sh&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;-f&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;$userresources&lt;/span&gt;&lt;span class=&quot;tok-s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;then&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;xrdb&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;-nocpp&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;-merge&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;$userresources&lt;/span&gt;&lt;span class=&quot;tok-s2&quot;&gt;&amp;quot;&lt;/span&gt;
&lt;span class=&quot;tok-k&quot;&gt;fi&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;According to &lt;code&gt;man xrdb&lt;/code&gt;:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;literalblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre&gt;-nocpp  This option indicates that xrdb should not run the  input  file
        through a preprocessor before loading it into properties.&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;It means, that &lt;code&gt;cpp&lt;/code&gt; preprocessor doesn&amp;#8217;t get called and that&amp;#8217;s why included files were not loaded.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;The fix is simple: just edit the &lt;code&gt;/etc/gdm/Xsession&lt;/code&gt; file and remove &lt;code&gt;-nocpp&lt;/code&gt; option from the command that loads
&lt;code&gt;.Xresources&lt;/code&gt;:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;sh&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;-f&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;$userresources&lt;/span&gt;&lt;span class=&quot;tok-s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-k&quot;&gt;then&lt;/span&gt;
&lt;span class=&quot;tok-w&quot;&gt;    &lt;/span&gt;xrdb&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;-merge&lt;span class=&quot;tok-w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;tok-s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;tok-nv&quot;&gt;$userresources&lt;/span&gt;&lt;span class=&quot;tok-s2&quot;&gt;&amp;quot;&lt;/span&gt;
&lt;span class=&quot;tok-k&quot;&gt;fi&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Maintainers decided to use &lt;code&gt;-nocpp&lt;/code&gt; flag because of performance. It seems that calling the &lt;code&gt;cpp&lt;/code&gt; preprocessor is quite
expensive operation. Most of users don&amp;#8217;t use &lt;code&gt;#include&lt;/code&gt;, &lt;code&gt;#define&lt;/code&gt;, etc. in their &lt;code&gt;.Xresources&lt;/code&gt; file. By using &lt;code&gt;-nocpp&lt;/code&gt;
you can decrease startup time of your graphic environment.&lt;/p&gt;
&lt;/div&gt;</description>
        <pubDate>Fri, 15 May 2015 00:00:00 +0000</pubDate>
        <link>https://manenko.com/2015/05/15/gdm-doesnt-load-included-files-from-xresources-in-arch-linux.html</link>
        <guid isPermaLink="true">https://manenko.com/2015/05/15/gdm-doesnt-load-included-files-from-xresources-in-arch-linux.html</guid>
        
        <category>GNU/Linux</category>
        
        <category>GNOME 3</category>
        
        <category>.Xresources</category>
        
        
      </item>
    
      <item>
        <title>Put UIDatePicker inside static UITableView</title>
        <description>&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;In the previous article we implemented input form using
&lt;a href=&quot;https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/index.html&quot;&gt;UITableView&lt;/a&gt;.
Static table views provide a great help during iOS applications
development: developer can visually create nice looking UIs. One of the
common tasks here is to create UI for picking a date. Apple already
provides UI element for this:
&lt;a href=&quot;https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIDatePicker_Class/index.html&quot;&gt;UIDatePicker&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;In this article we will embed &lt;code&gt;UIDatePicker&lt;/code&gt; into
the &lt;a href=&quot;https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewCell_Class/&quot;&gt;UITableViewCell&lt;/a&gt; inside
static table view. User will be able to show/hide it by tapping on a
label with date. Apple do this in their &lt;strong&gt;Reminders&lt;/strong&gt; application.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Let&amp;#8217;s reproduce this behavior.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;initial-setup&quot;&gt;Initial Setup&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Create a single view application, remove default view from the
&lt;strong&gt;Main.storyboard&lt;/strong&gt; and add &lt;strong&gt;Table View Controller&lt;/strong&gt; instead. And don&amp;#8217;t
forget to make it &lt;strong&gt;Initial View Controller&lt;/strong&gt;. Remove
&lt;strong&gt;ViewController.swift&lt;/strong&gt; file as well.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Now, select table view and set it&amp;#8217;s &lt;strong&gt;Content&lt;/strong&gt; to &lt;strong&gt;Static Cells&lt;/strong&gt;,
&lt;strong&gt;Style&lt;/strong&gt; to &lt;strong&gt;Grouped&lt;/strong&gt; and uncheck &lt;strong&gt;Show Selection on Touch&lt;/strong&gt;:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;imageblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;img src=&quot;/images/put-uidatepicker-inside-static-uitableview/uitableview-setup.png&quot; alt=&quot;Interface Builder: UITableView setup&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Set number of &lt;strong&gt;Sections&lt;/strong&gt; to &lt;strong&gt;3&lt;/strong&gt;. First section should have one row,
second section - 4 rows, third section - 2 rows. Select all rows and set
their &lt;strong&gt;Selection&lt;/strong&gt; property to &lt;strong&gt;None&lt;/strong&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;rows-setup&quot;&gt;Rows&apos; Setup&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;sect2&quot;&gt;
&lt;h3 id=&quot;firstrow&quot;&gt;First row&lt;/h3&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Select first row and set its style to &lt;strong&gt;Basic&lt;/strong&gt;. Double click on a label
inside the cell and enter any text. I use &quot;Write a letter to your future
self&quot;. This is a text for reminder.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect2&quot;&gt;
&lt;h3 id=&quot;secondrow&quot;&gt;Second row&lt;/h3&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Select second row and set its style to &lt;strong&gt;Custom&lt;/strong&gt;. Drag and drop one
label and one switch on it. Place label close to the left edge of the
row. Put switch close to the right edge.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Setup Auto Layout constraints for the switch:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;imageblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;img src=&quot;/images/put-uidatepicker-inside-static-uitableview/auto-layout-constraints-uiswitch.png&quot; alt=&quot;Interface Builder: Auto Layout constraints for UISwitch&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Setup Auto Layout constraints for the label:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;imageblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;img src=&quot;/images/put-uidatepicker-inside-static-uitableview/auto-layout-constraints-uilabel.png&quot; alt=&quot;Interface Builder: Auto Layout constraints for UILabel&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Double click on the label and set it&amp;#8217;s text to &quot;Remind me on a day&quot;. Set
&lt;strong&gt;State&lt;/strong&gt; of the switch to &lt;strong&gt;Off&lt;/strong&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect2&quot;&gt;
&lt;h3 id=&quot;thirdrow&quot;&gt;Third row&lt;/h3&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Select third row and set its style to the &lt;strong&gt;Right Detail&lt;/strong&gt;. Double click
on &lt;strong&gt;Title&lt;/strong&gt; label and set it&amp;#8217;s text to &quot;Alarm&quot;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Table view should have the following look right now:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;imageblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;img src=&quot;/images/put-uidatepicker-inside-static-uitableview/table-view-with-3-cells.png&quot; alt=&quot;Interface Builder: Table View with 3 cells&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect2&quot;&gt;
&lt;h3 id=&quot;fourthrow&quot;&gt;Fourth row&lt;/h3&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Select fourth row and resize it:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;imageblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;img src=&quot;/images/put-uidatepicker-inside-static-uitableview/resized-cell.png&quot; alt=&quot;Resized cell&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Drag and drop date picker on it and setup Auto Layout (just use &lt;strong&gt;Reset
to Suggested Constraints&lt;/strong&gt; on it):&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;imageblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;img src=&quot;/images/put-uidatepicker-inside-static-uitableview/date-picker-inside-uitableviewcell.png&quot; alt=&quot;Date picker inside UITableViewCell&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect2&quot;&gt;
&lt;h3 id=&quot;fifthrow&quot;&gt;Fifth row&lt;/h3&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Select fifth row and set it&amp;#8217;s style to &lt;strong&gt;Right Detail&lt;/strong&gt;. Set it&amp;#8217;s
&lt;strong&gt;Accessory&lt;/strong&gt; to &lt;strong&gt;Disclosure Indicator&lt;/strong&gt;. Double click on &lt;strong&gt;Title&lt;/strong&gt;
label and set its text to &quot;Repeat&quot;. Double click on &lt;strong&gt;Detail&lt;/strong&gt; label and
set its text to &quot;Never&quot;.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect2&quot;&gt;
&lt;h3 id=&quot;sixthrow&quot;&gt;Sixth row&lt;/h3&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Setup it in the same way as row #2. Set label text to &quot;Remind me at a
location&quot;.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect2&quot;&gt;
&lt;h3 id=&quot;seventhrow&quot;&gt;Seventh row&lt;/h3&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Set its style to &lt;strong&gt;Basic&lt;/strong&gt;. Set it&amp;#8217;s &lt;strong&gt;Accessory&lt;/strong&gt; to &lt;strong&gt;Disclosure
Indicator&lt;/strong&gt;. Double click on &lt;strong&gt;Title&lt;/strong&gt; label and set its text to
&quot;Location&quot;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;This is how your table view should look like now:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;imageblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;img src=&quot;/images/put-uidatepicker-inside-static-uitableview/table-view-with-all-cells.png&quot; alt=&quot;Table View with all cells&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;admonitionblock note&quot;&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;td class=&quot;icon&quot;&gt;
&lt;i class=&quot;fa icon-note&quot; title=&quot;Note&quot;&gt;&lt;/i&gt;
&lt;/td&gt;
&lt;td class=&quot;content&quot;&gt;
I decided not to copy the whole UI look and feel of the Reminders
application for simplicity.
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;listen-to-datepickers-value-changes&quot;&gt;Listen to DatePicker&amp;#8217;s value changes&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;We want to update text of the Detail label of third row each time user
changes value of the date picker.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Create a new Cocoa Touch Class named &lt;code&gt;TableViewController&lt;/code&gt; and set its
base class to &lt;code&gt;UITableViewController&lt;/code&gt;. Remove all auto generated code
inside newly created class:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;tok-nc&quot;&gt;UIKit&lt;/span&gt;

&lt;span class=&quot;tok-kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;tok-kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;tok-nc&quot;&gt;TableViewController&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-bp&quot;&gt;UITableViewController&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Open storyboard and select table view controller on it. Go to &lt;strong&gt;Identity
Inspector&lt;/strong&gt; and set &lt;strong&gt;Class&lt;/strong&gt; to &lt;code&gt;TableViewController&lt;/code&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Create outlets for detail label and date picker using &lt;strong&gt;Ctrl+Drag&lt;/strong&gt; in
&lt;strong&gt;Assistant Editor&lt;/strong&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-kr&quot;&gt;@IBOutlet&lt;/span&gt; &lt;span class=&quot;tok-kr&quot;&gt;weak&lt;/span&gt; &lt;span class=&quot;tok-kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;tok-nv&quot;&gt;txtDate&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-bp&quot;&gt;UILabel&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;!&lt;/span&gt;
&lt;span class=&quot;tok-kr&quot;&gt;@IBOutlet&lt;/span&gt; &lt;span class=&quot;tok-kr&quot;&gt;weak&lt;/span&gt; &lt;span class=&quot;tok-kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;tok-nv&quot;&gt;datePicker&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-bp&quot;&gt;UIDatePicker&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;!&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Add method that updates label text using date value from the date
picker:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-kr&quot;&gt;@IBAction&lt;/span&gt;
&lt;span class=&quot;tok-kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;tok-kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;tok-nf&quot;&gt;didChangeDate&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;tok-n&quot;&gt;txtDate&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;tok-bp&quot;&gt;NSDateFormatter&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;localizedStringFromDate&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;datePicker&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;date&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;dateStyle&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ShortStyle&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;timeStyle&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;ShortStyle&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Select date picker and go to &lt;strong&gt;Connections Inspector&lt;/strong&gt;. Drag from its
&lt;strong&gt;Value Changed&lt;/strong&gt; event to this method. Now add &lt;code&gt;viewDidLoad&lt;/code&gt; method
override:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;tok-kr&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;tok-kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;tok-nf&quot;&gt;viewDidLoad&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;tok-n&quot;&gt;didChangeDate&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;This will update label&amp;#8217;s text for the first time. Now build the project
and run it. If you did everything correct, then label should change its
text every time you change value of the date picker.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;toggledate-picker-on-date-rowtap&quot;&gt;Toggle date picker on date row tap&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;We want to toggle date picker when user taps on the cell with Alarm
date. Let&amp;#8217;s override
&lt;a href=&quot;https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/index.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView:didSelectRowAtIndexPath:&quot;&gt;tableView(_:didSelectRowAtIndexPath:)&lt;/a&gt; method:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;tok-kr&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;tok-kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;tok-nf&quot;&gt;tableView&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;tableView&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-bp&quot;&gt;UITableView&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;didSelectRowAtIndexPath&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;indexPath&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-bp&quot;&gt;NSIndexPath&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;tok-k&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;indexPath&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;section&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;indexPath&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;tok-k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;tok-mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;tok-n&quot;&gt;toggleDatePicker&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;tok-k&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;tok-p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;This will invoke &lt;code&gt;toggleDatePicker&lt;/code&gt; method if user tapped on the row
with index 1 in the section with index 1. And this is the row we need.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;How we should implement &lt;code&gt;toggleDatePicker&lt;/code&gt; method? We can try to set
row&amp;#8217;s &lt;code&gt;hidden&lt;/code&gt; property to &lt;code&gt;true&lt;/code&gt;. This doesn&amp;#8217;t work. We can try to
set it&amp;#8217;s height to zero. This also doesn&amp;#8217;t work.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;In order to achieve our goal we need to use
&lt;a href=&quot;https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/index.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView:heightForRowAtIndexPath:&quot;&gt;tableView(_:heightForRowAtIndexPath:)&lt;/a&gt;
method. This method provides heights for each table&amp;#8217;s row. So we need a
boolean property that holds information about visibility of the row and
then use it inside the method to return zero height if date picker
should not be visible:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;tok-kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;tok-nv&quot;&gt;datePickerHidden&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;tok-kc&quot;&gt;false&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;tok-kr&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;tok-kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;tok-nf&quot;&gt;tableView&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;tableView&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-bp&quot;&gt;UITableView&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;heightForRowAtIndexPath&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;indexPath&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-bp&quot;&gt;NSIndexPath&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;CGFloat&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;tok-k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;datePickerHidden&lt;/span&gt; &lt;span class=&quot;tok-o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;indexPath&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;section&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;tok-mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;tok-o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;indexPath&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;tok-mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;tok-k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;tok-mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;tok-k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;tok-k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;tok-kc&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;tableView&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;tableView&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;heightForRowAtIndexPath&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;indexPath&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;This method returns zero height for the date picker row if it should be
hidden.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;And now we can implement &lt;code&gt;toggleDatePicker&lt;/code&gt; method:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;tok-kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;tok-nf&quot;&gt;toggleDatePicker&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;tok-n&quot;&gt;datePickerHidden&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;tok-o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;datePickerHidden&lt;/span&gt;

    &lt;span class=&quot;tok-c1&quot;&gt;// Force table to update its contents&lt;/span&gt;
    &lt;span class=&quot;tok-n&quot;&gt;tableView&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;beginUpdates&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;tok-n&quot;&gt;tableView&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;endUpdates&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Date picker should be hidden when we load view for the first time:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;tok-kr&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;tok-kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;tok-nf&quot;&gt;viewDidLoad&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;tok-n&quot;&gt;didChangeDate&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;tok-n&quot;&gt;toggleDatePicker&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Compile and run&amp;#8230;&amp;#8203;&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;imageblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;img src=&quot;/images/put-uidatepicker-inside-static-uitableview/uidatetimepicker-display-issues.png&quot; alt=&quot;Display issues with UIDateTimePicker&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;To fix these display issues, you should go to storyboard, select row
that contains date picker and set it&amp;#8217;s &lt;strong&gt;Clip Subviews&lt;/strong&gt; property to
&lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Compile and run. Now it works as expected.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;refactoring&quot;&gt;Refactoring&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Let&amp;#8217;s stop for a moment and look into the code we have:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;tok-kr&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;tok-kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;tok-nf&quot;&gt;tableView&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;tableView&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-bp&quot;&gt;UITableView&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;heightForRowAtIndexPath&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;indexPath&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-bp&quot;&gt;NSIndexPath&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;CGFloat&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;tok-k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;datePickerHidden&lt;/span&gt; &lt;span class=&quot;tok-o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;indexPath&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;section&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;tok-mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;tok-o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;indexPath&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;tok-mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;tok-k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;tok-mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;tok-k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;tok-k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;tok-kc&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;tableView&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;tableView&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;heightForRowAtIndexPath&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;indexPath&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;tok-kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;tok-kr&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;tok-kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;tok-nf&quot;&gt;tableView&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;tableView&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-bp&quot;&gt;UITableView&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;didSelectRowAtIndexPath&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;indexPath&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-bp&quot;&gt;NSIndexPath&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;tok-k&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;indexPath&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;section&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;indexPath&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;tok-k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;tok-mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;tok-n&quot;&gt;toggleDatePicker&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;tok-k&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;tok-p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;I don&amp;#8217;t like &quot;magic&quot; numbers here. Also, I don&amp;#8217;t like duplication of row
identification logic. Let&amp;#8217;s fix this:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;tok-kd&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;tok-nc&quot;&gt;Row&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-nb&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;tok-k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;Reminder&lt;/span&gt;
    &lt;span class=&quot;tok-k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;RemindMeOnDay&lt;/span&gt;
    &lt;span class=&quot;tok-k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;Alarm&lt;/span&gt;
    &lt;span class=&quot;tok-k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;DatePicker&lt;/span&gt;
    &lt;span class=&quot;tok-k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;tok-nb&quot;&gt;Repeat&lt;/span&gt;
    &lt;span class=&quot;tok-k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;RemindMeAtLocation&lt;/span&gt;
    &lt;span class=&quot;tok-k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;Location&lt;/span&gt;

    &lt;span class=&quot;tok-k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;Unknown&lt;/span&gt;


    &lt;span class=&quot;tok-kd&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;indexPath&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-bp&quot;&gt;NSIndexPath&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;tok-kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;tok-nv&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;Row&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;Unknown&lt;/span&gt;

        &lt;span class=&quot;tok-k&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;indexPath&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;section&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;indexPath&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;tok-k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;tok-mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;tok-n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;Row&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;Reminder&lt;/span&gt;
        &lt;span class=&quot;tok-k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;tok-mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;tok-n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;Row&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;RemindMeOnDay&lt;/span&gt;
        &lt;span class=&quot;tok-k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;tok-mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;tok-n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;Row&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;Alarm&lt;/span&gt;
        &lt;span class=&quot;tok-k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;tok-mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;tok-n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;Row&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;DatePicker&lt;/span&gt;
        &lt;span class=&quot;tok-k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;tok-mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;tok-n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;Row&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-nb&quot;&gt;Repeat&lt;/span&gt;
        &lt;span class=&quot;tok-k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;tok-mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;tok-n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;Row&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;RemindMeAtLocation&lt;/span&gt;
        &lt;span class=&quot;tok-k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;tok-mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;tok-n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;Row&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;Location&lt;/span&gt;
        &lt;span class=&quot;tok-k&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;tok-p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;tok-bp&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;tok-o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;Row&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;Unknown&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;tok-kc&quot;&gt;self&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;row&lt;/span&gt;
    &lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;This enum replaces &quot;magic&quot; numbers with meaningful names, encapsulates
&lt;code&gt;NSIndex &amp;#8594; RowName&lt;/code&gt; transformation logic, protects from mistakes.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Let&amp;#8217;s rewrite our methods using this enum:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;tok-kr&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;tok-kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;tok-nf&quot;&gt;tableView&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;tableView&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-bp&quot;&gt;UITableView&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;didSelectRowAtIndexPath&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;indexPath&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-bp&quot;&gt;NSIndexPath&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;tok-kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;tok-nv&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;Row&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;indexPath&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;indexPath&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;tok-k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;Alarm&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;tok-n&quot;&gt;toggleDatePicker&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;tok-kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;tok-kr&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;tok-kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;tok-nf&quot;&gt;tableView&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;tableView&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-bp&quot;&gt;UITableView&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;heightForRowAtIndexPath&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;indexPath&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-bp&quot;&gt;NSIndexPath&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;CGFloat&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;tok-kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;tok-nv&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;Row&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;indexPath&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;indexPath&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;tok-k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;datePickerHidden&lt;/span&gt; &lt;span class=&quot;tok-o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;DatePicker&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;tok-mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-kc&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;tableView&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;tableView&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;heightForRowAtIndexPath&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;indexPath&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;This code is much better and less error prone.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;toggle-other-rows&quot;&gt;Toggle other rows&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Well, I will not show how to do this. It&amp;#8217;s your exercise.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Thanks for reading this.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;</description>
        <pubDate>Fri, 19 Dec 2014 00:00:00 +0000</pubDate>
        <link>https://manenko.com/2014/12/19/put-uidatepicker-inside-static-uitableview.html</link>
        <guid isPermaLink="true">https://manenko.com/2014/12/19/put-uidatepicker-inside-static-uitableview.html</guid>
        
        <category>iOS</category>
        
        <category>programming</category>
        
        <category>UIDatePicker</category>
        
        <category>UITableView</category>
        
        <category>UITableViewCell</category>
        
        <category>Auto Layout</category>
        
        <category>Swift</category>
        
        
      </item>
    
      <item>
        <title>How to create an input form using UITableView</title>
        <description>&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;&lt;a href=&quot;https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/index.html&quot;&gt;UITableView&lt;/a&gt; is a
powerful UI element that is used in various scenarios of iOS application development.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;In this article we will learn how to use it to build UI for input forms.  When we finish implementation, we will have
something similar to this:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;imageblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;img src=&quot;/images/how-to-create-an-input-form-using-uitableview/final-input-form.png&quot; alt=&quot;Final input form using UITableView&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;lets-get-started&quot;&gt;Let&amp;#8217;s get started&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Create a &lt;strong&gt;Single View Application&lt;/strong&gt; project, open &lt;strong&gt;Main.storyboard&lt;/strong&gt; and delete view controller that is already there.
Delete &lt;strong&gt;ViewController.swift&lt;/strong&gt; file as well.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Drag and drop &lt;strong&gt;Table View Controller&lt;/strong&gt; on the storyboard&amp;#8217;s surface and make it initial view controller using &lt;strong&gt;Attribute
Inspector&lt;/strong&gt;:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;imageblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;img src=&quot;/images/how-to-create-an-input-form-using-uitableview/tableviewcontroller-attributeinspector.png&quot; alt=&quot;Attribute Inspector: Is Initial View Controller&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;&lt;code&gt;UITableView&lt;/code&gt; can contain dynamic or static cells. We need the second one to build an input form:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;imageblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;img src=&quot;/images/how-to-create-an-input-form-using-uitableview/attributeinspector-static-cells.png&quot; alt=&quot;Attribute Inspector: Static Cells&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Go to &lt;strong&gt;Document Outline&lt;/strong&gt; and delete all table view cells except the first one.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;design-table-cell&quot;&gt;Design Table Cell&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;sect2&quot;&gt;
&lt;h3 id=&quot;label-view-and-text-field&quot;&gt;Label, View and Text Field&lt;/h3&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Now we can design our cell. Later we will copy/paste it to create more table cells with the same visual appearance.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Drag and drop one &lt;strong&gt;Label&lt;/strong&gt;, one &lt;strong&gt;View&lt;/strong&gt; and one &lt;strong&gt;Text Field&lt;/strong&gt; on the cell&amp;#8217;s surface. Set background color of the &lt;strong&gt;View&lt;/strong&gt; to
&lt;code&gt;#CCCCCC&lt;/code&gt;, and &lt;strong&gt;Alpha&lt;/strong&gt; to &lt;code&gt;0.5&lt;/code&gt;. Reorder and change sizes of UI elements to correspond to the image below:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;imageblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;img src=&quot;/images/how-to-create-an-input-form-using-uitableview/interfacebuilder-label-view-text-field.png&quot; alt=&quot;Interface Builder: Label&quot; width=&quot;View&quot; height=&quot;and Text Field&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Select &lt;strong&gt;Table View Cell&lt;/strong&gt; in the &lt;strong&gt;Document Outline&lt;/strong&gt; and set its background to &lt;code&gt;#FCFFE1&lt;/code&gt;, Then select the &lt;strong&gt;Label&lt;/strong&gt; and set
its font to &lt;code&gt;Caption 1&lt;/code&gt; and its color to &lt;code&gt;#42AEDD&lt;/code&gt;. Set its alignment to &lt;code&gt;Right&lt;/code&gt;. Now select the &lt;strong&gt;Text Field&lt;/strong&gt; and set
its border style to &lt;code&gt;None&lt;/code&gt;. Finally, set its font to &lt;code&gt;Body&lt;/code&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;You should have something similar to this now:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;imageblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;img src=&quot;/images/how-to-create-an-input-form-using-uitableview/interfacebuilder-customize-colors-fonts.png&quot; alt=&quot;Interface Builder: Customize colors and fonts of the table view cell&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect2&quot;&gt;
&lt;h3 id=&quot;auto-layout&quot;&gt;Auto Layout&lt;/h3&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Let&amp;#8217;s add
&lt;a href=&quot;https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/Introduction/Introduction.html&quot;&gt;Auto Layout&lt;/a&gt; 
constraints to the UI elements inside this cell.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;sect3&quot;&gt;
&lt;h4 id=&quot;label&quot;&gt;Label&lt;/h4&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Select label and click on &lt;strong&gt;Pin&lt;/strong&gt; button. Set constraints as shown in the screenshot below:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;imageblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;img src=&quot;/images/how-to-create-an-input-form-using-uitableview/auto-layout-constraints-label.png&quot; alt=&quot;Auto Layout constraints for the Label&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect3&quot;&gt;
&lt;h4 id=&quot;view&quot;&gt;View&lt;/h4&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Select view and click on &lt;strong&gt;Pin&lt;/strong&gt; button. Set constraints as shown in the screenshot:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;imageblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;img src=&quot;/images/how-to-create-an-input-form-using-uitableview/auto-layout-constraints-view.png&quot; alt=&quot;Auto Layout constraints for the View&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect3&quot;&gt;
&lt;h4 id=&quot;text-field&quot;&gt;Text Field&lt;/h4&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Now add constraints to the text field as shown in the screenshot:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;imageblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;img src=&quot;/images/how-to-create-an-input-form-using-uitableview/auto-layout-constraints-text-field.png&quot; alt=&quot;Auto Layout constraints for the Text Field&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect3&quot;&gt;
&lt;h4 id=&quot;update-frames&quot;&gt;Update Frames&lt;/h4&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Click on &lt;strong&gt;Resolve Auto Layout Issues&lt;/strong&gt; button and select &lt;strong&gt;Update Frames&lt;/strong&gt; menu item to make Interface Builder re-layout UI
elements according to added Auto Layout constraints:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;imageblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;img src=&quot;/images/how-to-create-an-input-form-using-uitableview/resolve-auto-layout-issues-update-frames.png&quot; alt=&quot;Resolve Auto Layout Issues: Update Frames&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Now our table view cell should look like this:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;imageblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;img src=&quot;/images/how-to-create-an-input-form-using-uitableview/auto-layout-constraints-in-action.png&quot; alt=&quot;Table View Cell: Auto Layout constraints in action&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;design-personal-info-section&quot;&gt;Design Personal Info Section&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Select &lt;strong&gt;Table View&lt;/strong&gt; and set its style to &lt;code&gt;Grouped&lt;/code&gt;:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;imageblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;img src=&quot;/images/how-to-create-an-input-form-using-uitableview/uitableview-grouped-style.png&quot; alt=&quot;UITableView: Grouped Style&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;This will make table view to show sections. Now select &lt;strong&gt;Table View Section&lt;/strong&gt; in the &lt;strong&gt;Document Outline&lt;/strong&gt; and set its
&lt;strong&gt;Header&lt;/strong&gt; property to &lt;code&gt;Personal Info.&lt;/code&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Select &lt;strong&gt;Table View Cell&lt;/strong&gt; in the &lt;strong&gt;Document Outline&lt;/strong&gt; and copy paste it four times. Now you should have five rows in the
&lt;strong&gt;Personal Info&lt;/strong&gt; section. Update text of labels inside these rows to values from the following list:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;olist arabic&quot;&gt;
&lt;ol class=&quot;arabic&quot;&gt;
&lt;li&gt;
&lt;p&gt;Nickname&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;First Name&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Last Name&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Password&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Password (again)&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Then select all table view cells and click on &lt;strong&gt;Update Frames&lt;/strong&gt;. Now you should have something like this:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;imageblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;img src=&quot;/images/how-to-create-an-input-form-using-uitableview/table-view-personal-info-section.png&quot; alt=&quot;Table View: Personal Info section&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;If labels&apos; fonts are different after the Copy/Paste - just close and reopen Xcode. This should fix the issue.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;design-contactinfo-section&quot;&gt;Design Contact Info Section&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Copy and paste previous section, change text, remove extra rows, update frames. This is how your table view should look
like:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;imageblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;img src=&quot;/images/how-to-create-an-input-form-using-uitableview/table-view-with-all-sections.png&quot; alt=&quot;Table View with all sections&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;make-labels-to-have-the-same-width&quot;&gt;Make Labels to Have the Same Width&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;As you noticed, all labels have different widths and because of that vertical lines have different positions. There is a
special Auto Layout constraint called &lt;strong&gt;Equal Widths&lt;/strong&gt;:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;imageblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;img src=&quot;/images/how-to-create-an-input-form-using-uitableview/auto-layout-equal-widths.png&quot; alt=&quot;Auto Layout: Equal Widths&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;This constraint makes all selected items to have the same width.  Unfortunately, it&amp;#8217;s not possible to apply it to the UI
elements, that have different root views.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;However it is possible to fix this in code:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;olist arabic&quot;&gt;
&lt;ol class=&quot;arabic&quot;&gt;
&lt;li&gt;
&lt;p&gt;Find label with the highest width value.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Add Auto Layout constraints to all labels to have exactly this width.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Create new &lt;strong&gt;Cocoa Touch Class&lt;/strong&gt;, and inherit it from &lt;code&gt;UITableViewController&lt;/code&gt;. Call it &lt;code&gt;TableViewController&lt;/code&gt;. Then select
&lt;strong&gt;Table View Controller&lt;/strong&gt; in the storyboard and set its class to &lt;code&gt;TableViewController&lt;/code&gt; in the &lt;strong&gt;Identity Controller&lt;/strong&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Go back to the &lt;code&gt;TableViewController&lt;/code&gt; class and remove all methods from it.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;The following method calculates label&amp;#8217;s width:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;tok-kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;tok-nf&quot;&gt;calculateLabelWidth&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-bp&quot;&gt;UILabel&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;CGFloat&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;tok-kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;tok-nv&quot;&gt;labelSize&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;sizeThatFits&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;CGSize&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;CGFloat&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-bp&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;frame&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;))&lt;/span&gt;

    &lt;span class=&quot;tok-k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;labelSize&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;width&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;I use 
&lt;a href=&quot;https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/index.html#//apple_ref/occ/instm/UIView/sizeThatFits:&quot;&gt;sizeThatFits(_:)&lt;/a&gt;
method of the &lt;a href=&quot;https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/index.html&quot;&gt;UIView&lt;/a&gt;
class to calculate size of the
&lt;a href=&quot;https://developer.apple.com/library/ios/documentation/UIKit/Reference/UILabel_Class/index.html&quot;&gt;UILabel&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;The next step is to find maximum width of labels:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;tok-kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;tok-nf&quot;&gt;calculateMaxLabelWidth&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;tok-bp&quot;&gt;UILabel&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;CGFloat&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;tok-k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;tok-bp&quot;&gt;reduce&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-bp&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;calculateLabelWidth&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;tok-mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;tok-bp&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;The next method adds Auto Layout constrains to constrain label widths:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;tok-kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;tok-nf&quot;&gt;updateWidthsForLabels&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;tok-bp&quot;&gt;UILabel&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;tok-kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;tok-nv&quot;&gt;maxLabelWidth&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;calculateMaxLabelWidth&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;tok-k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;label&lt;/span&gt; &lt;span class=&quot;tok-k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;labels&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;tok-kd&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;tok-nv&quot;&gt;constraint&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;tok-bp&quot;&gt;NSLayoutConstraint&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt;
                                       &lt;span class=&quot;tok-n&quot;&gt;attribute&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;Width&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt;
                                       &lt;span class=&quot;tok-n&quot;&gt;relatedBy&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;Equal&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt;
                                          &lt;span class=&quot;tok-n&quot;&gt;toItem&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-kc&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt;
                                       &lt;span class=&quot;tok-n&quot;&gt;attribute&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;NotAnAttribute&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt;
                                      &lt;span class=&quot;tok-n&quot;&gt;multiplier&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;,&lt;/span&gt;
                                        &lt;span class=&quot;tok-n&quot;&gt;constant&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-n&quot;&gt;maxLabelWidth&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;tok-n&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;addConstraint&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;constraint&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Now we need list of labels to apply this method to. Add outlet collection property:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-kr&quot;&gt;@IBOutlet&lt;/span&gt; &lt;span class=&quot;tok-kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;tok-nv&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;tok-bp&quot;&gt;UILabel&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;tok-o&quot;&gt;!&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;And link it with all labels on the input form:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;imageblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;img src=&quot;/images/how-to-create-an-input-form-using-uitableview/connect-outlet-collection-with-all-labels.png&quot; alt=&quot;Connect outlet collection with all labels&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Update widths in &lt;code&gt;viewDidLoad&lt;/code&gt;:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;listingblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre class=&quot;pygments highlight&quot;&gt;&lt;code data-lang=&quot;swift&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;tok-kr&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;tok-kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;tok-nf&quot;&gt;viewDidLoad&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;tok-p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;tok-n&quot;&gt;updateWidthsForLabels&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tok-n&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;tok-p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;tok-p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Compile and run:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;imageblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;img src=&quot;/images/how-to-create-an-input-form-using-uitableview/input-form-based-on-uitableview.png&quot; alt=&quot;Input Form based on UITableView&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;the-end&quot;&gt;The End&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;That&amp;#8217;s it. I hope that was useful.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;</description>
        <pubDate>Tue, 16 Dec 2014 00:00:00 +0000</pubDate>
        <link>https://manenko.com/2014/12/16/how-to-create-an-input-form-using-uitableview.html</link>
        <guid isPermaLink="true">https://manenko.com/2014/12/16/how-to-create-an-input-form-using-uitableview.html</guid>
        
        <category>iOS</category>
        
        <category>programming</category>
        
        <category>UITableView</category>
        
        <category>UITableViewController</category>
        
        <category>Auto Layout</category>
        
        <category>Swift</category>
        
        
      </item>
    
  </channel>
</rss>
