<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Michele Lindroos</title>
    <description>The latest articles on DEV Community by Michele Lindroos (@keelefi).</description>
    <link>https://dev.to/keelefi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F809341%2Fd4539e8b-6eee-465f-87a0-5dc923614ff5.jpeg</url>
      <title>DEV Community: Michele Lindroos</title>
      <link>https://dev.to/keelefi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/keelefi"/>
    <language>en</language>
    <item>
      <title>What is Metaprogramming?</title>
      <dc:creator>Michele Lindroos</dc:creator>
      <pubDate>Sun, 12 Jun 2022 13:57:47 +0000</pubDate>
      <link>https://dev.to/keelefi/what-is-metaprogramming-2lgb</link>
      <guid>https://dev.to/keelefi/what-is-metaprogramming-2lgb</guid>
      <description>&lt;p&gt;Already in my teenage years as a junior programmer I heard lots of talk about &lt;em&gt;Metaprogramming&lt;/em&gt;. Even though wikipedia didn't exist and information on the internet in general wasn't available to the same extent it is today, it was easy to look up the &lt;em&gt;definition&lt;/em&gt; of metaprogramming. My problem was that the definition didn't tell me much. Over the years I've learned a lot more about metaprogramming. In this blog post I'll explain what metaprogramming is. Furthermore, I'll show various examples of metaprogramming.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Definition of Metaprogramming
&lt;/h2&gt;

&lt;p&gt;Now, what is metaprogramming? When we're programming, i.e. write program code, we're writing code for the program. Conversely, when we're metaprogramming, we are writing code for the code itself.&lt;/p&gt;

&lt;p&gt;Sounds confusing? Guess how confused I was when I was 14yo and trying to learn all of this by myself...&lt;/p&gt;

&lt;p&gt;Examples often help alleviate confusion. Hence, I will give lots of examples throughout this post. Let's start with an example in pseudocode.&lt;/p&gt;

&lt;p&gt;Let's say we want to print the numbers from 1 to 10. Even a novice developer would think of using a loop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i = 1 to 10:
    print(i)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But would it be the same if we would write all the print statements out?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print(1)
print(2)
print(3)
print(4)
print(5)
print(6)
print(7)
print(8)
print(9)
print(10)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This question is more involved than first meets the eye. However, as compiler optimizations and instruction set architectures (ISA) are out-of-scope for this post, let's keep things simple and assume they will be executed as written. I.e. the former has more compact code, but the latter runs faster as it has no comparisons nor branches. A typical tradeoff between size and speed.&lt;/p&gt;

&lt;p&gt;However, code readability is useful, too. So, what if, we would like to write the former, but end up with the code from the latter? This is something metaprogramming could solve. For example, our pseudocode with metaprogramming might look as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;META_FOR(i, 1, 10):
    print(i)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In our pseudocode language this would generate the 10 sequential print-statements in code.&lt;/p&gt;

&lt;p&gt;Now that we have given the definition for metaprogramming and looked at an unrealistically simple example, let's look at some real examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  C Preprocessor Directives
&lt;/h2&gt;

&lt;p&gt;Many aspiring C programmers may not realize this at first, but the C preprocessor directives, such as &lt;code&gt;#include&lt;/code&gt; and &lt;code&gt;#define&lt;/code&gt; are metaprogramming. The C preprocessor, often abbreviated CPP, runs before the C compiler executes.&lt;/p&gt;

&lt;p&gt;The GNU C compiler (GCC) ships with the C preprocessor bundled. To run only the preprocessor, without running the compiler, we have to invoke &lt;code&gt;gcc&lt;/code&gt; with &lt;code&gt;-E&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;As a simple example, let's define a variable &lt;code&gt;VARIABLE_X&lt;/code&gt; as &lt;code&gt;5&lt;/code&gt; and use a print statement to print it. The code might look as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;define-x.c
&lt;span class="c"&gt;#define VARIABLE_X 5&lt;/span&gt;
&lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"X = %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;, VARIABLE_X&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's run the preprocessor and see the output. (We will have to use the switch &lt;code&gt;-P&lt;/code&gt; to avoid clutter caused by line markers the preprocessor would add by default):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;gcc &lt;span class="nt"&gt;-E&lt;/span&gt; &lt;span class="nt"&gt;-P&lt;/span&gt; define-x.c
&lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"X = %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;, 5&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The C preprocessor supports conditionals. We can write code that will end up different depending on definitions. For example, we could use &lt;code&gt;#ifdef&lt;/code&gt; to generate something when &lt;code&gt;OS_WINDOWS&lt;/code&gt; is defined and something else when &lt;code&gt;OS_LINUX&lt;/code&gt; is defined. Note that we need to end the conditional with &lt;code&gt;#endif&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;ifdef-windows-linux.c
&lt;span class="c"&gt;#ifdef OS_WINDOWS&lt;/span&gt;
&lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"hello Bill&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c"&gt;#endif&lt;/span&gt;
&lt;span class="c"&gt;#ifdef OS_LINUX&lt;/span&gt;
&lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"hello Linus&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c"&gt;#endif&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, if we don't set either, the generated code is empty:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;gcc &lt;span class="nt"&gt;-E&lt;/span&gt; &lt;span class="nt"&gt;-P&lt;/span&gt; ifdef-windows-linux.c
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we define &lt;code&gt;OS_WINDOWS&lt;/code&gt;, we get as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;gcc &lt;span class="nt"&gt;-E&lt;/span&gt; &lt;span class="nt"&gt;-P&lt;/span&gt; &lt;span class="nt"&gt;-D&lt;/span&gt; OS_WINDOWS ifdef-windows-linux.c
&lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"hello Bill&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and with &lt;code&gt;OS_LINUX&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;gcc &lt;span class="nt"&gt;-E&lt;/span&gt; &lt;span class="nt"&gt;-P&lt;/span&gt; &lt;span class="nt"&gt;-D&lt;/span&gt; OS_LINUX ifdef-windows-linux.c
&lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"hello Linus&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                                     
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can do mathematical statements in &lt;code&gt;#if&lt;/code&gt;'s:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="nt"&gt;-math&lt;/span&gt;.c
&lt;span class="c"&gt;#if (6 * 7) == 42&lt;/span&gt;
&lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"all good&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c"&gt;#else&lt;/span&gt;
&lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"is math broken?&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c"&gt;#endif                                                      &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output is as expected:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;gcc &lt;span class="nt"&gt;-E&lt;/span&gt; &lt;span class="nt"&gt;-P&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="nt"&gt;-math&lt;/span&gt;.c
&lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"all good&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In fact, the C preprocessor is very powerful. It is possible to do functions, loops and so forth. Now, it is important to understand that just because it is possible it doesn't mean it's a good idea. Some programs such as the linux kernel make heavy use of the C preprocessor, but most programs are best of with minimal use of it. Granted, you can't do much without &lt;code&gt;#include&lt;/code&gt; statements and header files need &lt;a href="https://gcc.gnu.org/onlinedocs/cppinternals/Guard-Macros.html"&gt;include guards&lt;/a&gt;, but otherwise I recommend using the C preprocessor sparingly.&lt;/p&gt;

&lt;h2&gt;
  
  
  C++ Templates
&lt;/h2&gt;

&lt;p&gt;C++ is a superset of C and therefore all of the C preprocessor macros work for C++, too. (Technically, the C preprocessor doesn't care about the source code at all and you could use the C preprocessor for whatever language you like)&lt;/p&gt;

&lt;p&gt;In addition to the C preprocessor, C++ has templates. Unlike the C preprocessor, the C++ templates are a native construct of the language. Despite this, the C++ templates are metaprogramming. C++ templates generate code that is to be compiled.&lt;/p&gt;

&lt;p&gt;Let's say we want to create a function that returns the sum of two integers. However, for whatever reason, we don't want to create a parameterized function taking to integers, but rather hard-code it as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;sum23&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, if we want to create many of these functions, we will have to write a lot of code. Instead, we could use templates to have the compiler create the code for us:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;N&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;M&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;template_sum&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;N&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;M&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To get an instance of the function &lt;code&gt;sum23()&lt;/code&gt;, we would do like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;sum23&lt;/span&gt;&lt;span class="p"&gt;)()&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;template_sum&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that preferrably we would do&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;sum23&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;template_sum&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;but I wanted to make the type explicit here for maximum readability.&lt;/p&gt;

&lt;p&gt;The most common usage of C++ template is containers. Say, you want to create a linked list container. In C++, without any kind of metaprogramming, i.e. without templates and the C preprocessor, you would have to rewrite the same container for every class you want it to support. In practice, would end up copy-pasting a lot of code which is suboptimal, to say the least.&lt;/p&gt;

&lt;p&gt;The declaration of our linked list implementation might look as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;linked_list&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The implementation of the linked-list is out-of-scope for this post. However, assuming we would create the implementation, then, we could create linked lists with integers, string etc.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;linked_list&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;my_integer_list&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;linked_list&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;my_string_list&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that the templates create an equivalent of source code that has been copy-pasted, i.e. there are separate classes for &lt;code&gt;linked_list&amp;lt;int&amp;gt;&lt;/code&gt;, &lt;code&gt;linked_list&amp;lt;std::string&amp;gt;&lt;/code&gt; etc. for everything that is instantiated anywhere. Furthermore, as compilation units, i.e. source code object files, are compiled separately, this can grow compilation times significantly. There are some optimizations to this, but those are out-of-scope for this post.&lt;/p&gt;

&lt;p&gt;While the C++ templates are extremely powerful, I would recommend using them sparingly. Myself, I make heavy use of the standard library containers as they're very good, but I rarely write any templates myself. Bugs related to templates are typically hard to solve. This is primarily because the compiler may not know if the bug is in the template code or in the usage of the template as either one could be the culprit.&lt;/p&gt;

&lt;h2&gt;
  
  
  Function Redefinition
&lt;/h2&gt;

&lt;p&gt;In a language like C, declaring a function assigns the symbol used to address the function. Every further declaration of the function needs to have the same prototype or otherwise the compiler complains. For example, let's say we declare &lt;code&gt;int my_sum(int, int)&lt;/code&gt; and later &lt;code&gt;int my_sum(char, char)&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;my_sum.h
&lt;span class="c"&gt;#pragma once&lt;/span&gt;
int my_sum&lt;span class="o"&gt;(&lt;/span&gt;int a, int b&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
int my_sum&lt;span class="o"&gt;(&lt;/span&gt;char a, char b&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The compiler throws an error as expected:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;gcc my_sum.c
In file included from my_sum.c:1:
my_sum.h:3:5: error: conflicting types &lt;span class="k"&gt;for&lt;/span&gt; ‘my_sum’
    3 | int my_sum&lt;span class="o"&gt;(&lt;/span&gt;char a, char b&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      |     ^~~~~~
my_sum.h:2:5: note: previous declaration of ‘my_sum’ was here
    2 | int my_sum&lt;span class="o"&gt;(&lt;/span&gt;int a, int b&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      |     ^~~~~~
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This makes sense. We want all our calls to &lt;code&gt;my_sum()&lt;/code&gt; to be unambiguous. More important, in a language like C, functions cannot be redefined. Even if we write the exact same implementation twice, as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;my_sum.c
&lt;span class="c"&gt;#include "my_sum.h"&lt;/span&gt;

int my_sum&lt;span class="o"&gt;(&lt;/span&gt;int a, int b&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;a + b&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

int my_sum&lt;span class="o"&gt;(&lt;/span&gt;int a, int b&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;a + b&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the compiler throws an error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;gcc my_sum.c
my_sum.c:8:5: error: redefinition of ‘my_sum’
    8 | int my_sum&lt;span class="o"&gt;(&lt;/span&gt;int a, int b&lt;span class="o"&gt;)&lt;/span&gt;
      |     ^~~~~~
my_sum.c:3:5: note: previous definition of ‘my_sum’ was here
    3 | int my_sum&lt;span class="o"&gt;(&lt;/span&gt;int a, int b&lt;span class="o"&gt;)&lt;/span&gt;
      |     ^~~~~~
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, while this helps avoid confusion, it wouldn't have to be like this. Instead of functions, let's consider normal variables in C. Let's assume we declare the integer &lt;code&gt;a&lt;/code&gt; and initially set it to &lt;code&gt;5&lt;/code&gt;. Later, we can set it to &lt;code&gt;7&lt;/code&gt; and that is totally allowed. Essentially, we are redefining variables:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;int_a.c 
&lt;span class="c"&gt;#include &amp;lt;assert.h&amp;gt;&lt;/span&gt;
int main&lt;span class="o"&gt;(&lt;/span&gt;void&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;{&lt;/span&gt;
    int a &lt;span class="o"&gt;=&lt;/span&gt; 5&lt;span class="p"&gt;;&lt;/span&gt;
    assert&lt;span class="o"&gt;(&lt;/span&gt;a &lt;span class="o"&gt;==&lt;/span&gt; 5&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    a &lt;span class="o"&gt;=&lt;/span&gt; 7&lt;span class="p"&gt;;&lt;/span&gt;
    assert&lt;span class="o"&gt;(&lt;/span&gt;a &lt;span class="o"&gt;==&lt;/span&gt; 5&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;return &lt;/span&gt;0&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Compiling and running this fails as expected:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;gcc int_a.c &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; ./a.out 
a.out: int_a.c:7: main: Assertion &lt;span class="sb"&gt;`&lt;/span&gt;a &lt;span class="o"&gt;==&lt;/span&gt; 5&lt;span class="s1"&gt;' failed.
Aborted (core dumped)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we change the latter assertion to &lt;code&gt;a == 7&lt;/code&gt;, the code runs fine.&lt;/p&gt;

&lt;p&gt;Why is it that we can redefine variables, but not functions? Are not the names we used to address functions not symbols just like the variables are?&lt;/p&gt;

&lt;p&gt;Indeed, and in many languages, such as python and javascript, function redefinition is perfectly allowed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Javascript Function Redefinition
&lt;/h2&gt;

&lt;p&gt;Let's talk about javascript. Let's assume we want to write a function &lt;code&gt;my_greeting()&lt;/code&gt; which returns a string containing a greeting message. For example, let's make the implementation as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;my_greeting&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, if we call the function and use the result as input for &lt;code&gt;console.log()&lt;/code&gt;, the expected message is printed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;my_greeting&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="nx"&gt;hello&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unlike in a language like C, we can redefine the function at will. Let's say we feel a little Spanish and want to change greeting accordingly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;my_greeting&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hola que tal&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Now, if we use the same code as before, it will call the new implementation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;my_greeting&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="nx"&gt;hola&lt;/span&gt; &lt;span class="nx"&gt;que&lt;/span&gt; &lt;span class="nx"&gt;tal&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Not only can we redefine standalone functions. We can also redefine functions belonging to an object. Let's say we have a class &lt;code&gt;Banana&lt;/code&gt; which has a function &lt;code&gt;getColor()&lt;/code&gt; which returns &lt;code&gt;'green'&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Banana&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;getColor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;green&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instantiating this class and calling &lt;code&gt;getColor()&lt;/code&gt; returns the expected result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;my_banana&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Banana&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;Banana&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;my_banana&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getColor&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;green&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we can change the implementation of the function. If we want to change the implementation of the single instance of the class, we can do as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;my_banana&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getColor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;yellow&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;Function&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;my_banana&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getColor&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;yellow&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead, if we want to change the implementation of all instances of the class, we need to change the class. To demonstate this, let's first create an instance of &lt;code&gt;Banana&lt;/code&gt; and call it &lt;code&gt;original_banana&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;original_banana&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Banana&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;Banana&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;original_banana&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getColor&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;green&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's change the implementation of the class method &lt;code&gt;getColor&lt;/code&gt;. To do that, we have to use &lt;code&gt;prototype&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;Banana&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getColor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;black&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's create a banana &lt;code&gt;new_banana&lt;/code&gt; and see that it has the new color:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;new_banana&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Banana&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;Banana&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;new_banana&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getColor&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;black&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Not only does the new banana have a new color, but the previously created banana uses the new implementation, too:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;original_banana&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getColor&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;black&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, recall that we changed the implementation of the instance of &lt;code&gt;my_banana&lt;/code&gt;. That implementation stays intact:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;my_banana&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getColor&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;yellow&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Python Function Redefinition
&lt;/h2&gt;

&lt;p&gt;In Python, we can redefine functions in a similar way as what we did in javascript. Let's do the greeting example in Python.  First, we define the original version of &lt;code&gt;my_greeting&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;my_greeting&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;'hello'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we can call it and print the result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_greeting&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="n"&gt;hello&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using the Python lambda syntax, we can redefine the function as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;my_greeting&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;lambda&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'hola que tal'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, if we call it, it uses the new implementation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_greeting&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="n"&gt;hola&lt;/span&gt; &lt;span class="n"&gt;que&lt;/span&gt; &lt;span class="n"&gt;tal&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Likewise, in Python, we can redefine functions of class instances. Let's define the class &lt;code&gt;Banana&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Banana&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_color&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;'green'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's create an instance of &lt;code&gt;Banana&lt;/code&gt;, call it &lt;code&gt;my_banana&lt;/code&gt; and call &lt;code&gt;get_color()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;my_banana&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Banana&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;my_banana&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get_color&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="s"&gt;'green'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, let's redefine &lt;code&gt;get_color&lt;/code&gt;. To do that we need to import &lt;code&gt;types&lt;/code&gt; and then call &lt;code&gt;types.MethodType&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;types&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;my_banana&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get_color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;types&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MethodType&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'yellow'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;my_banana&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;my_banana&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get_color&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="s"&gt;'yellow'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Changing the implementation for all instances of the class is similar. Instead of referencing the instance, we reference the class. Let's create an instance &lt;code&gt;original_banana&lt;/code&gt;, then change the implementation of &lt;code&gt;Banana.get_color&lt;/code&gt;, create the instance &lt;code&gt;new_banana&lt;/code&gt; and call all &lt;code&gt;get_color()&lt;/code&gt;'s:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;original_banana&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Banana&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Banana&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get_color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;types&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MethodType&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'black'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Banana&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;new_banana&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Banana&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;original_banana&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get_color&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="s"&gt;'black'&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;new_banana&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get_color&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="s"&gt;'black'&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;my_banana&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get_color&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="s"&gt;'yellow'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though &lt;code&gt;original_banana&lt;/code&gt; was instantiated before the redefinition of &lt;code&gt;get_color&lt;/code&gt;, it uses the new implementation. However, the custom implementation in &lt;code&gt;my_banana.get_color&lt;/code&gt; stays intact.&lt;/p&gt;

&lt;h2&gt;
  
  
  Python Dynamic Classes
&lt;/h2&gt;

&lt;p&gt;In Python, we have a concept of &lt;em&gt;metaclassses&lt;/em&gt;. Using metaclasses, we can dynamically create classes.&lt;/p&gt;

&lt;p&gt;Metaclasses have rather niche utility. As such, they are needed mostly in library development or similar. I will show an example of how metaclasses could be used. However, the example is rather long and, more importantly, not a recommended way of doing things. With the disclaimer out of the way, we are ready to proceed.&lt;/p&gt;

&lt;p&gt;Let's say we want to create classes for various car models. The conventional (and preferred) way would be to create a class "Car" which would then include various information related to the car such as &lt;code&gt;engine&lt;/code&gt;, &lt;code&gt;price&lt;/code&gt; and whatever is needed. Furthermore, for our example, let's say we also have classses for the manufacturer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;engine&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;engine&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Manufacturer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's say we get some data according to which create instances, i.e. objects, of our classes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;cars_dict&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s"&gt;'Audi'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s"&gt;'A5'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;'engine'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'ICE'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="s"&gt;'price'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;46000&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="s"&gt;'S5'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;'engine'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'ICE'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="s"&gt;'price'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;55000&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="s"&gt;'e-tron'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;'engine'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'electric'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'price'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;102000&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="s"&gt;'Porsche'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s"&gt;'911'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;'engine'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'ICE'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'price'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;214000&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="s"&gt;'Taycan'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;'engine'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'electric'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'price'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;194000&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This data could then be read in a function as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_cars_conventional&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cars_dict&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;cars&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;manufacturer&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;cars_dict&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;models&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;cars_dict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;manufacturer&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
            &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cars_dict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;manufacturer&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="s"&gt;'engine'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
                                  &lt;span class="n"&gt;cars_dict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;manufacturer&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="s"&gt;'price'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

        &lt;span class="n"&gt;cars&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;manufacturer&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Manufacturer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;cars&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We could call this function and thereafter we could query the various instances for data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;cars&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;create_cars_conventional&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cars_dict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;audi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cars&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'Audi'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;a5&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;audi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'A5'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;a5&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;engine&lt;/span&gt;
&lt;span class="s"&gt;'ICE'&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;a5&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;price&lt;/span&gt;
&lt;span class="mi"&gt;46000&lt;/span&gt;               
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For what it is worth, we could also directly query information of specific models:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;cars&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'Audi'&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'A5'&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;engine&lt;/span&gt;
&lt;span class="s"&gt;'ICE'&lt;/span&gt;                      
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, what if we would like to create classes rather than instances of the cars? We can do that with metaprogramming. As classes are first-class citizens in Python, a class is an object. To create a class object, we call &lt;code&gt;type()&lt;/code&gt; with the class name, the inherited base classes and the dictionary. For instance, our "Audi A5" class might look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;A5&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'A5'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'ICE'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;46000&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and now we can use it like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;A5&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;engine&lt;/span&gt;
&lt;span class="s"&gt;'ICE'&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, we would probably like to have a function like &lt;code&gt;create_cars_conventional&lt;/code&gt; which builds all classes for us. It could be as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_cars_classes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cars_dict&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;classes_all&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;types&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SimpleNamespace&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;manufacturer&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;cars_dict&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;classes_models&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;cars_dict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;manufacturer&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
            &lt;span class="n"&gt;class_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'Car{}{}'&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;manufacturer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;car_engine&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cars_dict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;manufacturer&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="s"&gt;'engine'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="n"&gt;car_price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cars_dict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;manufacturer&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="s"&gt;'price'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="n"&gt;car_class&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;class_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;car_engine&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;car_price&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
            &lt;span class="nb"&gt;setattr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;classes_all&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;class_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;car_class&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;classes_all&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now it can be used like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;cars&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;create_cars_classes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cars_dict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;mycar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cars&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CarAudiA5&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;mycar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;engine&lt;/span&gt;
&lt;span class="s"&gt;'ICE'&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;mycar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;price&lt;/span&gt;
&lt;span class="mi"&gt;46000&lt;/span&gt;                                        
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As said, this is mainly a niche feature which most programmers will never need. But it has it's use in library development and so forth.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this blog post I gave a definition for &lt;em&gt;metaprogramming&lt;/em&gt;. Furthermore, I showed examples of what qualifies as metaprogramming. As we moved forward towards more complicated examples, the distinction between what is and isn't metaprogramming got less obvious. Ultimately, I wanted to show that metaprogramming can get somewhat esoteric. Moreover, I wanted to show that metaprogramming is rarely needed and solutions without metaprogramming should be considered first. Metaprogramming is mostly useful in library and similar scenarios, where we are effectively extending the programming language itself.&lt;/p&gt;

</description>
      <category>metaprogramming</category>
      <category>preprocessor</category>
      <category>metaclass</category>
    </item>
    <item>
      <title>I wrote the same program in 5 different languages, this is what I learned</title>
      <dc:creator>Michele Lindroos</dc:creator>
      <pubDate>Thu, 31 Mar 2022 06:36:15 +0000</pubDate>
      <link>https://dev.to/keelefi/i-wrote-the-same-program-in-5-different-languages-this-is-what-i-learned-2312</link>
      <guid>https://dev.to/keelefi/i-wrote-the-same-program-in-5-different-languages-this-is-what-i-learned-2312</guid>
      <description>&lt;p&gt;For years I've wanted to have code I've written myself that can I look at when I need to write some projects from scratch. As I was working on &lt;a href="https://github.com/keelefi/jox/"&gt;jox&lt;/a&gt;, I noticed that the graph solving algorithm it needs would be an excellent program to write in different languages. In this blog post I document the different things I learned as I wrote the various implementations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;I started programming at the age of 10 and this year I'm turning 35 and thus this is my 25th year of programming. As a teenager I wrote code for myself. Over the years I collected a vast codebase that I could reuse as I started something new.&lt;/p&gt;

&lt;p&gt;This changed in 2008 as I started working on proprietary code for a company. Since 2012 I've worked as a full-time employee for various companies. Especially as I've switched from one company to another, I've often longed for the old code that I've written and that is now inaccessible to me.&lt;/p&gt;

&lt;p&gt;Some years ago I got the idea of creating a set of code skeletons for various languages. While I had a clear need for it, the idea didn't sit too well with me. I had a clear idea that I want the skeletons to include at least build scripts and unit testing. However, the idea rubbed me in the wrong way as writing a silly program that does not solve any real world problem might be far too simple as reference.&lt;/p&gt;

&lt;p&gt;As a side note, for many years now I've disliked how programming languages are often introduced by showing a simple hello world program. It shows some basic syntax of a language, but syntax isn't really the interesting part. Things like idioms and paradigms, metaprogramming support, error signalling and unit testing are what I care about.&lt;/p&gt;

&lt;p&gt;As I was working on the design of the job dependency resolver algorithm in &lt;code&gt;jox&lt;/code&gt;, I realized it was an excellent choice for creating sample code. I need unit tests for verification the test works. The algorithm is a graph algorithm. The graph can have loops, which typically complicates things somewhat. Furthermore, the algorithm would need to be able to detect faulty input, report the error and exit gracefully.&lt;/p&gt;

&lt;p&gt;I wanted to write the algorithm in python, because python is often useful for many things and I've written a bunch of python already. I wanted to include an implementation in javascript, because it's a popular choice of language in many places. Furthermore, I used &lt;code&gt;jest&lt;/code&gt; in a project and felt it was a rather nice unit testing library and I wanted to play around with it in my spare time.&lt;/p&gt;

&lt;p&gt;I wanted to add an implementation in C++, because it has for many years been my favorite langauge. Also, as I've written many builds in CMake, I wanted to try out using Autotools. You can find my blog post detailing what I learned about Autoconf here: &lt;a href="https://dev.to/keelefi/my-autoconf-primer-1jkb"&gt;My Autoconf Primer&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Furthermore, Golang is currently a somewhat popular choice. Moreover, the list of successful projects written in Golang is impressive: Docker, Kubernetes and Terraform, just to name a few that I've used extensively myself, which are open-source.&lt;/p&gt;

&lt;p&gt;Last but not least, I wanted to write an implementation in Scheme, because I've lately become interested in declarative programming and lambda calculus. Thus, I've been practicing Scheme.&lt;/p&gt;

&lt;p&gt;To reiterate, the languages I wrote the algorithm in are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;python&lt;/li&gt;
&lt;li&gt;javascript&lt;/li&gt;
&lt;li&gt;C++&lt;/li&gt;
&lt;li&gt;Golang&lt;/li&gt;
&lt;li&gt;Scheme&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As I wanted to test all the languages with the same tests, I created a unified test vector set using JSON. Now, if I want to modify test data, I need to only update JSON files and tests for all languages are updated synchronously.&lt;/p&gt;

&lt;p&gt;Nowadays GitHub provides native test pipelines on their site called GitHub Action workflows. As I've written GitHub Actions previously, I decided to use them here as well. Therefore, all compilable code is compiled in GitHub workflows and all tests are run in GitHub workflows. Obviously, you can run the tests locally, too.&lt;/p&gt;

&lt;p&gt;The repository for this project can be found here: &lt;a href="https://github.com/keelefi/jox-algorithm"&gt;https://github.com/keelefi/jox-algorithm&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note: At the time of writing this post, the Scheme implementation is still work-in-progress. Also, I might later add more languages. However, more importantly, I plan to update the code samples if I learn I'm using bad or outdated patterns etc.&lt;/p&gt;

&lt;p&gt;In this section I presented the goals of the exercise. Furthermore, I described the choice of programming languages and skimmed through some related info. In the following sections I describe the various things I learned while doing this exercise, one per section, respectively.&lt;/p&gt;

&lt;h2&gt;
  
  
  JSON data should be formatted as objects
&lt;/h2&gt;

&lt;p&gt;As described in the background section, I used JSON files to store test data. I had input test vectors under &lt;code&gt;"input"&lt;/code&gt; and output test vectors under &lt;code&gt;"output"&lt;/code&gt;, respectively. However, under both of them, I put more keys, creating a hierarchy I expected would mimic the code I want to have. In other words, if I had input vectors &lt;code&gt;"jobA"&lt;/code&gt; and &lt;code&gt;"jobB"&lt;/code&gt;, the JSON file would look something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
"input": { 
     "jobA": { ... },     
     "jobB": { ... } 
}, 
... 
} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is exactly how I ended up writing the code. However, there are JSON parsing libraries where the library automatically creates objects for you depending on the JSON data. These libraries expect lists of objects, instead of keys to objects as I used. Namely, I should've formatted the example JSON data as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
"input": [     
     { "name": "jobA", ... }, 
     { "name": "jobB",  ... } 
], 
...  
} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this scenario I would've been able to fully use the automatically generated objects the various libraries provide.&lt;/p&gt;

&lt;h2&gt;
  
  
  Build setups are hard in every language
&lt;/h2&gt;

&lt;p&gt;Already as a teenager I learned that setting up the build and dependencies in a new project can be cumbersome. At the time, I was only interested in creating builds on my own machine. As a I grew older and more professional, I got more interested in build reproducibility and having the build setup work on any machine. The increase in complexity as you want to have reproducible builds cannot be overstated. It is hard, and it is hard in every language.&lt;/p&gt;

&lt;p&gt;I started my exercise writing the python build. I've used &lt;code&gt;pip&lt;/code&gt;, &lt;code&gt;virtualenv&lt;/code&gt; and &lt;code&gt;pyenv&lt;/code&gt; and I was expecting this would be easy. I spent lots of time doing research online and ultimately I came to the conclusion it seems very hard to create a fully isolated build environment in python. Ultimately I gave up and just wrote a &lt;code&gt;README.md&lt;/code&gt; containing instructions to install a few dependencies using &lt;code&gt;pipenv&lt;/code&gt;. As this exercise is rather simple, this build is unlikely to fail, but then again, I failed to create solid reference material here.&lt;/p&gt;

&lt;p&gt;With javascript I used &lt;code&gt;npm&lt;/code&gt;. &lt;code&gt;npm&lt;/code&gt; is very easy to setup. You use &lt;code&gt;npm init&lt;/code&gt; to create a &lt;code&gt;package.json&lt;/code&gt; where you populate dependencies and scripts for the build. In my case the only dependency was &lt;code&gt;jest&lt;/code&gt;, which I use for unit testing. However, even though &lt;code&gt;npm&lt;/code&gt; was very simple to use in this case, there are severe problems w.r.t. reproducibility and security when using &lt;code&gt;npm&lt;/code&gt;. Packages can disappear and malicious packages can be uploaded to the package repository. Hence, like the solution I used for building python, the solution for javascript is not optimal, either.&lt;/p&gt;

&lt;p&gt;With C++ I used Autoconf. As mention in the background section, I wrote a separate blog post on this experience. Autoconf was confusing to learn and I cannot recommend it to any proprietary inhouse projects where the build environment and target system are well-known. However, for open-source software where the integration is done by the user (or software distributor, such as a linux distribution like Debian or Fedora), Autoconf is very powerful.&lt;/p&gt;

&lt;p&gt;Golang has it's own compiler which also ships the unit test executor. Golang was the easiest build setup in my scenario. I wrote a minimal &lt;code&gt;go.mod&lt;/code&gt; containing one external dependency. The &lt;code&gt;go.mod&lt;/code&gt; also contains the compiler version that is needed for compilation. Furthermore, running &lt;code&gt;go&lt;/code&gt; will create a &lt;code&gt;go.sum&lt;/code&gt; that, according to best practices, is uploaded in the git repository. This practice creates some extra commits in the repository as the dependencies are upgraded to newer versions. For this to work efficiently, you want good test coverage so that you can just bump versions as they're released. While I acknowledge this is opinionated, I think it's a good thing. Then again, I don't think this works nearly as well in FOSS scenarios, but I think the authors of Golang never had FOSS in mind. In fact, as far as I know, Golang compiles all code into a single binary and uses no dynamically linkable libraries.&lt;/p&gt;

&lt;p&gt;Lastly I had Scheme. There's a multitude of implementations of Scheme interpreters. I use &lt;a href="https://www.gnu.org/software/guile/"&gt;GNU Guile&lt;/a&gt; myself. I worry that my code would not work as well with other Scheme interpreters. Furthermore, Scheme is extended with SRFI's - Scheme Requests For Implementation. The SRFI's are numbered proposals for the various Scheme implementations to implement. In practice, your interpreter might ship with the SRFI you are looking for. If you're looking for a less common SRFI, you may have to find one from a separate source. But not all dependencies are SRFI's. In my case I wanted to use &lt;code&gt;guile-json&lt;/code&gt; and this alone likely breaks the code on all other Scheme interpreters.&lt;/p&gt;

&lt;p&gt;The Scheme SRFI system is actually very nice on a theoretical level. Anyone can create an SRFI, there's a clear way to improve the SRFI and after it's finalized, all the various interpreters can implement the SRFI as they see fit. This is extremely decentralized which is great. However, I see problems in the distribution of the SRFI implementations. If the interpreter doesn't ship the SRFI you are looking for, where do you get the SRFI? I can get it in many different ways, but there is no standardized way and that can be a headache for build reproducibility.&lt;/p&gt;

&lt;h2&gt;
  
  
  GitHub Action workflows are good and bad
&lt;/h2&gt;

&lt;p&gt;I've worked with many CI systems over the years and I have to admit that GitHub Action workflows are by far the simplest and fastest to setup a simple build and test pipeline in. Of course, if you don't know the syntax like I do, it can take some time to setup a pipeline. But for me it took very minimal time to setup building and testing for all five languages.&lt;/p&gt;

&lt;p&gt;The builds are not that straightforward, either. In the C++ build I use the distribution package manager extensively to install packages. In the Scheme build I clone the &lt;code&gt;guile-json&lt;/code&gt; git repository. In all builds I use language specific tools for compiling (where applicable) and running the tests.&lt;/p&gt;

&lt;p&gt;However, while it's easy to setup the steps to build and run tests, it's dramatically less simple to visualize the results.  I would like to have a status matrix showing for each language the state of build and tests, respectively. In other words, for example, I would like to see at a glance if the C++ build works and if the C++ tests work, respectively. Even after spending considerable amount of time on this, I found no feasible way to achieve this. Instead, each workflow shows either "pass" or "failure", consolidating all steps into a single result.&lt;/p&gt;

&lt;p&gt;For what it's worth, this is not unlike my previous experiences with GitHub Action workflows. When using Jenkins, it is very easy to get advanced build and test status visualization. For instance, you can show how many tests pass out of tests total. As far as I know, this is currently not possible to achieve in GitHub Actions. As such, I think GitHub Action workflows are a good fit for small FOSS projects like this exercise, but for serious software development you want something more advanced, even though the initial setup is more involved.&lt;/p&gt;

&lt;h2&gt;
  
  
  Python is great for algorithm prototyping and design
&lt;/h2&gt;

&lt;p&gt;Already before I went to University, I used to sketch on paper (or in my head) the algorithms I needed to implement. At the University, this practice was reenforced and I liked to do UML's and whatnot. However, as I wrote increasingly complex algorithms, this practice didn't seem as feasible. I would end up with the best solution faster by just writing code and iterating the code.&lt;/p&gt;

&lt;p&gt;Not all languages are alike in this regard. As I worked on this exercise, it became more apparent to me how different python and C++ are in this regard. I like C++ in many ways, but in C++ you need to write a considerable amount of boilerplate and making just trivial changes to data structures typically requires a magnitude more changes compared to similar changes in python.&lt;/p&gt;

&lt;p&gt;There are many reasons for this discrepancy. One of the reasons is that the syntax of C++ is more expressive. In C++, even though we are working with a high-level language, we know exactly if we will get a hashmap (e.g. &lt;code&gt;unordered_map&lt;/code&gt;), red-black tree (e.g. &lt;code&gt;map&lt;/code&gt; or &lt;code&gt;set&lt;/code&gt;) etc. In python you don't need to worry about this, which can be inefficient in production, but great for sketching.&lt;/p&gt;

&lt;p&gt;Indeed, sketching out the algorithm design in python turned out very productive for me. As I hit a dead end, it didn't take much to make suitable changes and to get up to speed again. As such, python may be a good choice for prototyping in many scenarios.&lt;/p&gt;

&lt;h2&gt;
  
  
  Metaprogramming is awesome
&lt;/h2&gt;

&lt;p&gt;Not only was it easy to sketch the algorithm proper in python, but also many things related to testing were easy in python and javascript as they have very extensive metaprogramming support. By comparison, the metaprogramming capabilities of C++ are less, but still rather powerful. In my experience, this is not the case in Golang, and it's painful.&lt;/p&gt;

&lt;p&gt;For example, in all three of python, javascript and C++ I could have separate test cases in their own, dynamically generated, functions. In Golang I found no option for dynamic functions for every test data file and instead I had to create one function that reads the test vector files and then runs all of the tests. For what it's worth, this is also what I ended up doing in Scheme, but I'd say that is more because I don't know Scheme that well.&lt;/p&gt;

&lt;p&gt;Metaprogramming is such a powerful tool that I plan on writing a separate blog post on it. I think that metaprogramming capabilities are a crucial feature of a programming language. With metaprogramming you can work around many otherwise nasty problems you are facing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Exceptions are nice, but not necessary
&lt;/h2&gt;

&lt;p&gt;All four other languages I used in this exercise support exceptions, but Golang does not. Instead, in Golang you return errors. This is similar to how you propagate errors in C. I know that many C++ programmers shy away from exceptions, but I've always liked them in all languages.&lt;/p&gt;

&lt;p&gt;Indeed, I feel that also in this exercise the solutions in python, javascript and C++ were nicer than the the Golang solution due to the use of exceptions. When the algorithm faces an unrecoverable error scenario, it can just throw an exception. In contrast, with Golang I needed to add checks for function calls to see if an error occured.&lt;/p&gt;

&lt;p&gt;This is obviously opinionated, and proponents of the Golang design will tell you that it's great that the caller of a function make it explicit that the function can return an error. While I tend to appreciate some explicitness, over the years I've learned to like it less.&lt;/p&gt;

&lt;p&gt;That being said, the Golang solution for this exercise did not turn out as nasty as I was worried it would. Checking the errors in Golang doesn't clutter the code nearly as much I was expecting. Personally, I prefer exceptions, but I can live with the error propagating system present in Golang.&lt;/p&gt;

&lt;h2&gt;
  
  
  You are most productive in the language you know
&lt;/h2&gt;

&lt;p&gt;This is an obvious point to make, but this was so emphasized during my exercise that I want to mention it. Even if you know the language syntax, if you are not familiar with idioms and patterns of a language, it will take a lot more time to write the code. I was, and still am, most familiar with python, javascript and C++. As I was writing code in these languages, I focused almost exclusively on the algorithm and the libraries I was using.&lt;/p&gt;

&lt;p&gt;Compared to the three previously mentioned languages, with Golang I have far less mileage. For example, I wrote some structs, then later I had to rewrite them as I realized the idiomatic way to solve things in Golang are different. Golang is not a complicated language, but reaching the full solution required a lot of rewriting as I learned my earlier choices were bad. I also expect I will have to improve the Golang code in the future more than the three other implementations.&lt;/p&gt;

&lt;p&gt;Scheme is the one I have the least mileage with. In fact, even though I spent more time on Scheme than any other language, I didn't get even close to the full solution. However, I don't think Scheme is harder in itself. I think Scheme is magnitudes simpler than C++. The thing is that I'm used to imperative programming and Scheme is based on lambda calculus, which is fundamentally different. The way I see it, I have over 20 years worth of experience writing imperative code and less than 1 year worth of experience writing lambda calculus. Therefore, I'm currently very slow in writing Scheme.&lt;/p&gt;

&lt;h2&gt;
  
  
  Popular languages have an advantage in stack overflow questions and answers
&lt;/h2&gt;

&lt;p&gt;Writing python and javascript was fast not only because I knew the languages, but also because I could ask google any question that popped in my head and a related stack overflow question and answer would be available, respectively. The answers are not poor quality, either, but rather, they often explain the background and guide you to idiomatic solutions and solid patterns, which I find very nice.&lt;/p&gt;

&lt;p&gt;As I moved on to write the Scheme solution, this became very clear. Many questions I wanted to ask had not been asked anywhere. I had to do a lot more work to find the relevant information I was looking for. This slowed me down considerably. However, more importantly, I'm also less confident my solutions and the patterns I'm using in Scheme are good. Frankly, I feel they aren't.&lt;/p&gt;

&lt;p&gt;For what it is worth, as I was stuck with the Scheme implementation, I even ended up creating a stack overflow account so that I could ask a question myself. It was a nice experience: I asked my question on a Sunday morning and a few hours later it was already answered. The answer was off the topic, but I asked clarifying questions and the same guy ultimately answered my original question. I hope my question will be useful to someone else, too!&lt;/p&gt;

&lt;h2&gt;
  
  
  I hope to learn more Scheme and lambda calculus
&lt;/h2&gt;

&lt;p&gt;Even though I didn't finish the Scheme implementation, it was fun and exciting to write it. Pretty much everything is different in Scheme compared to the mostly imperative languages I'm used to. While it was rather easy to write the algorithm in python (and then copy the solution to javascript, C++ and Golang), I'm not too happy with the result. As I have extensive testing, I know the python solution works correctly. But it's not beautiful, and, in fact, it might not be optimal, either.&lt;/p&gt;

&lt;p&gt;Over this blog post, I've described the other languages as &lt;em&gt;imperative&lt;/em&gt;. However, the division between imperative and declarative is not clearcut. For example, C was clearly designed as an imperative language. It was meant to be an abstraction layer for assembly code. The original use case was to compile C to different assembly languages.&lt;/p&gt;

&lt;p&gt;While the original use case still stands (even though most code now exclusively runs on x86), processors are now far more complex and compilers are way more powerful at optimizing. Instead of executing the C program the programmer wrote, the compiler and processor will run the equivalent, but optimized, program. This is especially important when considering multi-threaded programs running on multicore systems. For more information on the topic, I suggest the interested reader watch the talk &lt;a href="https://herbsutter.com/2013/02/11/atomic-weapons-the-c-memory-model-and-modern-hardware/"&gt;Atomic weapons&lt;/a&gt; by Herb Sutter.&lt;/p&gt;

&lt;p&gt;I think that Scheme and lambda calculus has still a lot to give to me and the programming industry in general. In fact, I feel like over the decades we are slowly moving away from the computation model Alan Turing envisioned and toward lambda calculus, as described by Alonzo Church. For what it's worth, Turing machines and lambda calculus are equivalent.&lt;/p&gt;

&lt;p&gt;As I've understood, many hope that functional programming would make simultaneous multiprocessing more feasible. While I personally don't see the connection that strong, I think that writing declarative code is more productive and meaningful compared to the imperative counterpart. After all, at the end of the day, we have the computer make mathematical calculations, and we don't care what middle steps it does, as long as it ultimately returns the solution to our question. This is fundamentally declarative, not imperative.&lt;/p&gt;

&lt;h2&gt;
  
  
  Future work
&lt;/h2&gt;

&lt;p&gt;Obviously, as the Scheme implementation is still work-in-progress, I want to learn more Scheme and I have fun writing it, I hope to finish the Scheme implementation. Currently, I'm stuck working on a depth-first-search algorith in Scheme.&lt;/p&gt;

&lt;p&gt;As I mentioned earlier in the blog post, I'm planning on writing a blog post about metaprogramming. I think it would be fun and useful. Similarly, I might write something about the differences between imperative and declarative programing, as I'm currently quite interested in the topic.&lt;/p&gt;

&lt;p&gt;Also, I hope to keep updating the &lt;code&gt;jox-algorithm&lt;/code&gt; repository relevant to this blog post. I hope to update the build setups as I learn more about reproducible builds, as I think that would be very useful. I might also change from GitHub Action workflows to something else. Also, as I learn better programming patterns, I hope to fix any bad or outdated patterns I'm currently using.&lt;/p&gt;

&lt;p&gt;I don't expect anyone to make pull requests, but I'm open to pull requests in case someone finds this exercise useful as reference material when starting new projects in various languages.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this blog post I describe my hobby project I've been sporadically working on for a few months or so. I wrote the same code in five different languages: python, javascript, C++, Golang and Scheme. In this blog post I describe various things I learned while doing the exercise. If you found this blog post useful or have opinions or questions about my points, feel free to comment below. I also accept pull request to the repository if you find it useful as reference material.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>My Autoconf Primer</title>
      <dc:creator>Michele Lindroos</dc:creator>
      <pubDate>Wed, 09 Feb 2022 20:43:23 +0000</pubDate>
      <link>https://dev.to/keelefi/my-autoconf-primer-1jkb</link>
      <guid>https://dev.to/keelefi/my-autoconf-primer-1jkb</guid>
      <description>&lt;p&gt;Since a young age I've learned that build systems and dependency management range from difficult to mind-bogglingly frustrating. My best experiences have been with Autotools, which might surprise many. However, I had never made anything serious from scratch using Autotools myself, but rather only modified things setup by others. Lately I had some positive experiences building C programs linking &lt;code&gt;libguile&lt;/code&gt; with Autoconf and this prompted me to try something bigger. I setup the build toolchain for a C++ program using Autotools. In this blog post I attempt to document what I learned along the way.&lt;/p&gt;

&lt;h2&gt;
  
  
  Autotools, Automake and Autoconf
&lt;/h2&gt;

&lt;p&gt;The first question I wanted answered is if I should be using Autotools, Autoconf or even Automake. The answer is that Autotools is not a binary or similar, but rather it's an umbrella term for Autoconf, Automake and everything related. On the other hand, &lt;code&gt;automake&lt;/code&gt; is a binary that converts a &lt;code&gt;Makefile.am&lt;/code&gt; into &lt;code&gt;Makefile.in&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In my brand new program, I needed to learn very little about Automake. I created a top-level &lt;code&gt;Makefile.am&lt;/code&gt; containing a pointer to the related documentation and an instruction to recurse into the subdirectory &lt;code&gt;src&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cat Makefile.am
SUBDIRS = src
dist_doc_DATA = README.md
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The file in &lt;code&gt;src/Makefile.am&lt;/code&gt;  contains information of the sources to compile into objects and the resulting binaries:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cat src/Makefile.am 
bin_PROGRAMS = test_algorithm
test_algorithm_SOURCES = \
    algorithm.cpp \
    exception.cpp \
    job.cpp \
    warning.cpp \
    test_algorithm.cpp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You can run &lt;code&gt;automake&lt;/code&gt; as a standalone program. However, I found it far more convenient to call it using &lt;code&gt;autoreconf&lt;/code&gt;. &lt;code&gt;autoreconf&lt;/code&gt; is a program that runs &lt;code&gt;automake&lt;/code&gt; and &lt;code&gt;autoconf&lt;/code&gt; (and more) conveniently for you. Typically, I run &lt;code&gt;autoreconf&lt;/code&gt; with the following switches: &lt;code&gt;-i&lt;/code&gt; to generate missing auxiliary files, &lt;code&gt;-v&lt;/code&gt; for verbose output and &lt;code&gt;-f&lt;/code&gt; to force regeneration of all configuration files.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ autoreconf -vif
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;But now I'm getting ahead of myself. Before we can run &lt;code&gt;autoreconf&lt;/code&gt;, we need to write the input for &lt;code&gt;autoconf&lt;/code&gt;, namely, &lt;code&gt;configure.ac&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction to Autoconf
&lt;/h2&gt;

&lt;p&gt;The role of Autoconf is to generate the &lt;code&gt;configure&lt;/code&gt; to ship with a software distribution. The input file &lt;code&gt;configure.ac&lt;/code&gt; is formatted in the macro language &lt;a href="https://www.gnu.org/software/m4/m4.html"&gt;M4&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now, this might be a bit confusing, but &lt;code&gt;configure&lt;/code&gt; (and hence &lt;code&gt;autoconf&lt;/code&gt;) is not meant to work with software dependency management. Instead, the main design principle of Autoconf is to work with features and capabilities of the installed system where the distributed software is compiled.&lt;/p&gt;

&lt;h2&gt;
  
  
  config.h
&lt;/h2&gt;

&lt;p&gt;Although I didn't use it myself, I need to mention &lt;code&gt;config.h&lt;/code&gt;. (for what it is worth, I should probably try it out and update this part of the blog post later)&lt;/p&gt;

&lt;p&gt;As a result of running &lt;code&gt;configure&lt;/code&gt;, a header file &lt;code&gt;config.h&lt;/code&gt; is typically generated. This header file contains detected capabilities of the system we are running the compilation on. For example, if we look for &lt;code&gt;pthread&lt;/code&gt;, &lt;code&gt;config.h&lt;/code&gt; will contain suitable &lt;code&gt;#define&lt;/code&gt;'s to indicate if &lt;code&gt;pthread&lt;/code&gt; is found and linkable or not.&lt;/p&gt;

&lt;p&gt;As stated previously, I did not utilize &lt;code&gt;config.h&lt;/code&gt; and therefore no such file is generated in my case.&lt;/p&gt;

&lt;h2&gt;
  
  
  Structure of configure.ac
&lt;/h2&gt;

&lt;p&gt;The first statement of &lt;code&gt;configure.ac&lt;/code&gt; must be &lt;code&gt;AC_INIT&lt;/code&gt; and the last must be &lt;code&gt;AC_OUTPUT&lt;/code&gt;. The rest of the statements can be placed anywhere, except for when they depend on each other. This dynamic seems to have created a de facto standard structure for &lt;code&gt;configure.ac&lt;/code&gt; files. Even the Autoconf manual has a section &lt;a href="https://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.71/html_node/Autoconf-Input-Layout.html"&gt;Standard &lt;code&gt;configure.ac&lt;/code&gt; Layout&lt;/a&gt;. However, I find it quite confusing and instead I follow a mental picture, which is as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Initialization&lt;/li&gt;
&lt;li&gt;Compiler settings&lt;/li&gt;
&lt;li&gt;Configuration Files&lt;/li&gt;
&lt;li&gt;Dependencies&lt;/li&gt;
&lt;li&gt;Output&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As previously stated, &lt;code&gt;AC_INIT&lt;/code&gt; is the first statement. It accepts parameters such as program name, program version, maintainer e-mail address etc. In my case it is:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;AC_INIT([algorithm], [1.0])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;As part of the initialization phase, I also put a statement to initialize &lt;code&gt;automake&lt;/code&gt; with &lt;code&gt;AM_INIT_AUTOMAKE&lt;/code&gt;. I found this in the &lt;a href="https://www.gnu.org/software/automake/manual/html_node/amhello_0027s-configure_002eac-Setup-Explained.html"&gt;&lt;code&gt;automake&lt;/code&gt; manual&lt;/a&gt;. Note that statements starting with &lt;code&gt;AC_&lt;/code&gt; are for Autoconf and statements starting with &lt;code&gt;AM_&lt;/code&gt; are macros for Automake. However, &lt;code&gt;AM_INIT_AUTOMAKE&lt;/code&gt; is the only statement for Automake in my case.&lt;/p&gt;

&lt;p&gt;Now, this is really important: We give some parameters that look just like compiler flags, but they are for Automake when it generates &lt;code&gt;Makefile.in&lt;/code&gt; etc. I used &lt;code&gt;-Wall&lt;/code&gt;, &lt;code&gt;-Werror&lt;/code&gt; and &lt;code&gt;foreign&lt;/code&gt;. I needed &lt;code&gt;foreign&lt;/code&gt;, because otherwise Automake requires all the files in a standard GNU package, i.e. &lt;code&gt;NEWS&lt;/code&gt;, &lt;code&gt;README&lt;/code&gt;, &lt;code&gt;AUTHORS&lt;/code&gt; and &lt;code&gt;ChangeLog&lt;/code&gt;. My &lt;code&gt;AM_INIT_AUTOMAKE&lt;/code&gt; is as follows:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;AM_INIT_AUTOMAKE([-Wall -Werror foreign])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Compiler settings
&lt;/h2&gt;

&lt;p&gt;Next we need to select a compiler. When compiling a standard C program, we use &lt;code&gt;AC_PROG_CC&lt;/code&gt;. In my case, I'm compiling C++ so I use &lt;code&gt;AC_PROG_CXX&lt;/code&gt; instead. Note that the macro &lt;code&gt;AC_PROG_CPP&lt;/code&gt; is related to the C preprocessor and has nothing to do with C++ (for the C++ preprocessor command, use the macro &lt;code&gt;AC_PROG_CXXCPP&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;The only parameter that &lt;code&gt;AC_PROG_CC&lt;/code&gt; and &lt;code&gt;AC_PROG_CXX&lt;/code&gt; accept are a compiler search list, but no compiler flags. Instead, if you want to specify compiler flags, you need to set &lt;code&gt;CFLAGS&lt;/code&gt; or &lt;code&gt;CXXFLAGS&lt;/code&gt; before calling the macro. As per the Autoconf manual, by default &lt;code&gt;AC_PROG_{CC,CXX}&lt;/code&gt; sets &lt;code&gt;{C,CXX}FLAGS}&lt;/code&gt; to &lt;code&gt;-g -O2&lt;/code&gt; (or &lt;code&gt;-g&lt;/code&gt; if the detected compiler is not a GNU compiler). But if &lt;code&gt;{C,CXX}FLAGS&lt;/code&gt; was already set, it's kept unchanged.&lt;/p&gt;

&lt;p&gt;In my case, I call the macro &lt;code&gt;AC_PROG_CXX&lt;/code&gt; with the following construct:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;: ${CXXFLAGS="-g -O2 -Wall -Werror"}
AC_PROG_CXX
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Furthermore, as my source code uses C++17 features, I use an extra macro for that. But it's a little special and I will come back to it later in the section Autoconf Archive.&lt;/p&gt;

&lt;h2&gt;
  
  
  Configuration files
&lt;/h2&gt;

&lt;p&gt;As explained earlier, Automake compiles an &lt;code&gt;Makefile.in&lt;/code&gt; from &lt;code&gt;Makefile.am&lt;/code&gt;. However, this &lt;code&gt;Makefile.in&lt;/code&gt; is not yet a &lt;code&gt;Makefile&lt;/code&gt;. To specify which makefiles need to be compiled, we use the macro &lt;code&gt;AC_CONFIG_FILES&lt;/code&gt;. In my case:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;AC_CONFIG_FILES([
    Makefile
    src/Makefile
])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Note that we will still distribute the &lt;code&gt;Makefile.in&lt;/code&gt; and the &lt;code&gt;Makefile&lt;/code&gt; will be generated only on site. The instructions to build the file are contained in &lt;code&gt;config.status&lt;/code&gt; while the &lt;code&gt;configure&lt;/code&gt; will use.&lt;/p&gt;

&lt;p&gt;Furthermore, the utility of &lt;code&gt;AC_CONFIG_FILES&lt;/code&gt; is not retricted to makefiles, but other files can be specified, too. However, I had no such use.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dependencies
&lt;/h2&gt;

&lt;p&gt;As explained earlier, Autoconf doesn't work with software dependency managers in the conventional sense. I'll call this section &lt;em&gt;Dependencies&lt;/em&gt; anyhow, as that's how I think about it. There are various things you can do here, and in my case I was happy I needed 3 different ways to search for libraries and headers, as that really made me learn.&lt;/p&gt;

&lt;h3&gt;
  
  
  Dependencies: AC_CHECK_LIB
&lt;/h3&gt;

&lt;p&gt;The most typical thing you would do is &lt;code&gt;AC_CHECK_LIB&lt;/code&gt;. To &lt;code&gt;AC_CHECK_LIB&lt;/code&gt; you give the library name, a symbol in the library, and, optionally, a code segment to execute on success and a code segment to execute on failure. For example, in my scenario I ended up doing&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;AC_CHECK_LIB([pthread], [pthread_create], [], [AC_MSG_ERROR([pthread is not installed.])])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;My first surprise was that a symbol from the library is needed. In fact, the macro will expand into a C program that will be compiled. The source of the C program will contain the symbol we indicated, i.e. &lt;code&gt;pthread_create&lt;/code&gt; and the program will be linked against the library, i.e. with the flag &lt;code&gt;-lpthread&lt;/code&gt;. If compilation succeeds, the third parameter will be executed, but since its &lt;code&gt;[]&lt;/code&gt; the default action will be taken. Note that to disable the default action you can do &lt;code&gt;[ ]&lt;/code&gt;. If compilation fails, we run the fourth parameter which is a macro to signal an error and halt operation.&lt;/p&gt;

&lt;p&gt;The default action is to add the specified library to &lt;code&gt;LIBS&lt;/code&gt;, i.e. in my case &lt;code&gt;LIBS="-lpthread  $LIBS" (yes, I double-checked from the generated&lt;/code&gt;configure&lt;code&gt;). Furthermore, the define&lt;/code&gt;HAVE_LIBlibrary&lt;code&gt;will be set. In my case, it's&lt;/code&gt;#define HAVE_LIBPTHREAD 1`.&lt;/p&gt;

&lt;p&gt;I think there's at least two takeaways here. First of all, we don't match libraries with any kind of versioning. Instead, we look for a symbol. I would assume this can cause headache with missing or deprecated features. Furthermore, the macro &lt;code&gt;AC_CHECK_LIB&lt;/code&gt; uses &lt;em&gt;always&lt;/em&gt; C linking. This means that C++ libraries cannot be added using this construct, unless the library also has some symbols using C linking (i.e. &lt;code&gt;extern "C"&lt;/code&gt;).&lt;/p&gt;

&lt;h3&gt;
  
  
  Dependencies: Linking C++ libraries
&lt;/h3&gt;

&lt;p&gt;Since we can't use &lt;code&gt;AC_CHECK_LIB&lt;/code&gt; for linking C++ code, what other options do we have? There's a macro &lt;code&gt;AX_CXX_CHECK_LIB&lt;/code&gt;, but that is not distributed as part of Autoconf and not even the Autoconf Archive, so I have decided against using it.&lt;/p&gt;

&lt;p&gt;Instead, I found a &lt;a href="https://nerdland.net/2009/07/detecting-c-libraries-with-autotools/"&gt;blog post&lt;/a&gt; that suggests writing your own check. The basic idea is to use the macro &lt;code&gt;AC_LINK_IFELSE&lt;/code&gt; (this is the same macro as &lt;code&gt;AC_CHECK_LIB&lt;/code&gt; uses). To &lt;code&gt;AC_LINK_IFELSE&lt;/code&gt; you give a source to compile and a segment to run on success and a segment to run on failure. In my case:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;AC_LINK_IFELSE(
    &amp;lt;source here&amp;gt;,
    [HAVE_&amp;lt;library&amp;gt;=1],
    [AC_MSG_ERROR([&amp;lt;library&amp;gt; is not installed.])])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;To generate the source code that is used to verify linking, the blog post suggests using the macro &lt;code&gt;AC_LANG_PROGRAM&lt;/code&gt;. The macro takes to parameters, a prologue and a body. In my case:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;AC_LANG_PROGRAM([#include &amp;lt;gtest/gtest.h&amp;gt;], [EXPECT_EQ(1,1)])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We are still missing two pieces here. First, even though we set &lt;code&gt;AC_PROG_CXX&lt;/code&gt;, we have never set the language to use when running compilation tests. This is done using the macro &lt;code&gt;AC_LANG&lt;/code&gt;. By setting &lt;code&gt;AC_LANG(C++)&lt;/code&gt;, &lt;code&gt;AC_LINK_IFELSE&lt;/code&gt; will use a C++ compiler (and linker) rather than the default choice of a C compiler.&lt;/p&gt;

&lt;p&gt;Furthermore, on success, &lt;code&gt;AC_CHECK_LIB&lt;/code&gt; will add the specified library to the variable &lt;code&gt;LIBS&lt;/code&gt;. Therefore, we have to do it manually. If we would want to be really fancy, we could unset the library from &lt;code&gt;LIBS&lt;/code&gt;, if the test fails. Since we exit on failure, we don't unset anything.&lt;/p&gt;

&lt;p&gt;Here's the whole block of code:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;AC_LANG(C++)
LIBS="-lgtest $LIBS"
AC_LINK_IFELSE(
    [AC_LANG_PROGRAM([#include &amp;lt;gtest/gtest.h&amp;gt;], [EXPECT_EQ(1,1)])],
    [HAVE_GTEST=1],
    [AC_MSG_ERROR([gtest is not installed.])])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Dependencies: Headers
&lt;/h3&gt;

&lt;p&gt;Some dependencies are merely headers. To check the availability of a header, there's the macro &lt;code&gt;AC_CHECK_HEADER&lt;/code&gt;. This macro takes the header as it would be used in a &lt;code&gt;#include&lt;/code&gt; preprocessor directive. Furthermore, you can specify an action to take on success and on failure, respectively. In my case, I'm using the header-only library &lt;code&gt;nlohmann-json&lt;/code&gt; and only headers from &lt;code&gt;gmock&lt;/code&gt;, and thus I'm using the following two statements:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;AC_CHECK_HEADER([nlohmann/json.hpp], [], [AC_MSG_ERROR([nlohmann-json is not installed.])])
AC_CHECK_HEADER([gmock/gmock.h], [], [AC_MSG_ERROR([gmock is not installed.])])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Dependencies: pkg-config
&lt;/h3&gt;

&lt;p&gt;The idea behind &lt;code&gt;pkg-config&lt;/code&gt; seems simple enough: it's a program that can be used to query for installed software and their versions, respectively. &lt;code&gt;pkg-config&lt;/code&gt; requires software to be distributed with package metadata contained in a &lt;code&gt;.pc&lt;/code&gt; file. The metadata file will contain various information of the package, such as &lt;code&gt;CFLAGS&lt;/code&gt; and &lt;code&gt;LIBS&lt;/code&gt;. Sounds like a replacement for &lt;code&gt;AC_CHECK_LIB&lt;/code&gt;, right?&lt;/p&gt;

&lt;p&gt;The Autoconf Archives provides the macro &lt;code&gt;AX_PKG_CHECK_MODULES&lt;/code&gt;. However, on various online forums and blogs, I found warnings about how Autoconf and &lt;code&gt;pkg-config&lt;/code&gt; have a fundamentally different approach. Therefore, I decided against using &lt;code&gt;pkg-config&lt;/code&gt; in my setup.&lt;/p&gt;

&lt;p&gt;As I understand it, one problem is that merely the success or failure of running &lt;code&gt;pkg-config&lt;/code&gt; does not adequately indicate if linking the package works. For example, if &lt;code&gt;pkg-config&lt;/code&gt; is not installed, the test may fail, even though the dependency we are looking for exists and is linkable. Likewise, there can be a discrepancy between how &lt;code&gt;pkg-config&lt;/code&gt; and our compiler/linker of choice works. Lastly, the information in the &lt;code&gt;.pc&lt;/code&gt; file can be wrong. The last point is probably relevant when chrooting or similar.&lt;/p&gt;
&lt;h2&gt;
  
  
  All of configure.ac
&lt;/h2&gt;

&lt;p&gt;In the previous sections, I've explained all of my &lt;code&gt;configure.ac&lt;/code&gt;, with the exception of &lt;code&gt;AC_OUTPUT&lt;/code&gt;. There's not much to it: we just have to call it in the end of &lt;code&gt;configure.ac&lt;/code&gt;, as it will generate the files needed for Automake.&lt;/p&gt;

&lt;p&gt;My whole &lt;code&gt;configure.ac&lt;/code&gt; is as follows (comments included):&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cat configure.ac
AC_INIT([algorithm], [1.0])
AM_INIT_AUTOMAKE([-Wall -Werror foreign])

: ${CXXFLAGS="-g -O2 -Wall -Werror"}
AC_PROG_CXX

# check that compiler supports c++17 and set -std=c++17
AX_CXX_COMPILE_STDCXX_17([noext], [mandatory])

AC_CONFIG_FILES([
    Makefile
    src/Makefile
])

AC_CHECK_LIB([pthread], [pthread_create], [], [AC_MSG_ERROR([pthread is not installed.])])

# AC_CHECK_LIB doesnt work well with C++ linking. Instead, we use a test program to see if linking of gtest works.
AC_LANG(C++)
LIBS="-lgtest $LIBS"
AC_LINK_IFELSE(
    [AC_LANG_PROGRAM([#include &amp;lt;gtest/gtest.h&amp;gt;], [EXPECT_EQ(1,1)])],
    [HAVE_GTEST=1],
    [AC_MSG_ERROR([gtest is not installed.])])

# nlohmann-json is a header only library.
AC_CHECK_HEADER([nlohmann/json.hpp], [], [AC_MSG_ERROR([nlohmann-json is not installed.])])

# we need gmock headers even though we don't link it
AC_CHECK_HEADER([gmock/gmock.h], [], [AC_MSG_ERROR([gmock is not installed.])])

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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Recap on Usage
&lt;/h2&gt;

&lt;p&gt;Alright, so you've created all the needed source files and now you wonder what commands to use? First, we do&lt;br&gt;
&lt;code&gt;autoreconf&lt;/code&gt; to generate &lt;code&gt;configure&lt;/code&gt;, &lt;code&gt;Makefile.in&lt;/code&gt; and many more files:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ autoreconf -vif
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Note: This step is only done by package maintainers.&lt;/p&gt;

&lt;p&gt;To the end-user, we ship all of &lt;code&gt;configure&lt;/code&gt;, &lt;code&gt;Makefile.in&lt;/code&gt;'s etc. The first command the end-user does is to run the&lt;br&gt;
&lt;code&gt;configure&lt;/code&gt; script:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ./configure
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If we, as the package maintainers, did everything correctly, running &lt;code&gt;configure&lt;/code&gt; at the end-user will either fail, if&lt;br&gt;
the program (or library) would never be able to compile or run properly. Likewise, assuming we did everything correctly, &lt;br&gt;
if &lt;code&gt;configure&lt;/code&gt; succeeds, then also compiling and running the program (or library) will work.&lt;/p&gt;

&lt;p&gt;To build the source:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ make
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Note that if we did everything correctly, overriding compiler flags with &lt;code&gt;CXXFLAGS&lt;/code&gt; (or &lt;code&gt;CFLAGS&lt;/code&gt; if C program), linkable&lt;br&gt;
libraries with &lt;code&gt;LIBS&lt;/code&gt; etc will work properly.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Autoconf Archives
&lt;/h2&gt;

&lt;p&gt;The Autoconf Archives is a separate software distribution from Autoconf itself. It contains a multitude of macros that can be used when generating the &lt;code&gt;configure&lt;/code&gt;. Note that it's not an issue to depend on the Autoconf Archives, as we don't distribute the &lt;code&gt;configure.ac&lt;/code&gt;, but instead we distribute &lt;code&gt;configure&lt;/code&gt;. Hence, the end-user needs neither Autoconf nor the Autoconf Archives.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this post I went through what I learned as I setup a C++ project which is built with Autoconf (and Automake). Autoconf is quite different from CMake and other build systems. For this reason it's probably off-putting for newcomers as many things seem unnecessarily complicated. However, in my experience, well-written &lt;code&gt;configure&lt;/code&gt; files tend to work far better on various systems compared to many other solutions. I doubt I've reached that level of understanding, but everyone needs to start somewhere :-)&lt;/p&gt;

&lt;p&gt;While it was definitely frustrating to figure out many things about Autoconf, I'm happy I did it. I feel I understand the world of package management and software distribution a tad better.&lt;/p&gt;

</description>
      <category>autoconf</category>
      <category>autotools</category>
      <category>automake</category>
    </item>
  </channel>
</rss>
