<story><title>Go and Rust – objects without class</title><url>https://lwn.net/SubscriberLink/548560/26d15e832d21a483/</url></story><parent_chain></parent_chain><comment><author>usea</author><text>I&apos;ve been developing medium-to-large projects in mostly C# over the past two years. One thing about my style that has changed over that course of time is that I almost never use inheritance anymore. When I was in school, it was my primary tool for code reuse, but now I find it conflates solutions to different problems, as well as hampering readability.&lt;p&gt;My style now is small classes with a few, small methods each and lots of composition. I find it easier to reason about code, separate functionality, and reuse pieces. Also, placing unrelated state in separate classes really discourages a lot of the common mistakes with OOP.&lt;p&gt;Admittedly I don&apos;t do as much reading on the subject as I should. I am probably discovering things that were common knowledge 25 years ago. Maybe this is a common road that developers take. That&apos;s fine with me.&lt;p&gt;Rust&apos;s and Go&apos;s OOP style appeals to me quite a bit. I have only written small things in those languages, so I&apos;m not exactly well-versed in what larger projects look like. Before reading this article, I wasn&apos;t able to put into words what bothered me about the style of inheritance in languages I&apos;ve used up until now.</text></comment>
<story><title>Go and Rust – objects without class</title><url>https://lwn.net/SubscriberLink/548560/26d15e832d21a483/</url></story><parent_chain></parent_chain><comment><author>mixedbit</author><text>I&apos;ve been recently watching Structure and Interpretation of Computer programs, where it is demonstrated that first class functions and assignment operator with nested scope are enough to implement object oriented system.&lt;p&gt;The simplest example given is a Counter object. Translated from Lisp to Javascript it goes like this:&lt;p&gt;&lt;pre&gt;&lt;code&gt;      function make_counter() {
        var val = 0;
        function get() {
          return val;
        }
        function inc() {
          val += 1;
        }
        return [get, inc]  
      }
    
      function get_count(counter) {
        return counter[0]();
      }
    
      function inc_count(counter) {
        counter[1]();
      }
    
      var counter = make_counter();
      console.log(get_count(counter).toString());
      inc_count(counter);
      inc_count(counter);
      console.log(get_count(counter).toString());
&lt;/code&gt;&lt;/pre&gt;
This gives polymorphism (you can define make_fast_counter() that increases by 2 and still use get_count() and inc_count() with it) and encapsulation (you can not decrease the count).</text></comment>