<?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: Jack Delahunt</title>
    <description>The latest articles on DEV Community by Jack Delahunt (@jackdelahunt).</description>
    <link>https://dev.to/jackdelahunt</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%2F886273%2F730d3420-5b0c-414d-93fc-6e624618a0a6.jpeg</url>
      <title>DEV Community: Jack Delahunt</title>
      <link>https://dev.to/jackdelahunt</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jackdelahunt"/>
    <language>en</language>
    <item>
      <title>Fixing C++ by making unions better</title>
      <dc:creator>Jack Delahunt</dc:creator>
      <pubDate>Tue, 12 Jul 2022 18:23:34 +0000</pubDate>
      <link>https://dev.to/jackdelahunt/fixing-c-by-making-unions-better-f47</link>
      <guid>https://dev.to/jackdelahunt/fixing-c-by-making-unions-better-f47</guid>
      <description>&lt;p&gt;In the past, I have gone through some of my issues with C++ and how I think I can improve the language. To do this I am currently creating my own language called ‘Liam’. I have gone over basic syntax and features in a past post here but today I am focusing on the new “Typed Unions” feature.&lt;/p&gt;

&lt;p&gt;A full walkthrough of this in video form is on my channel &lt;a href="https://youtu.be/ORJ8v8542qo"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We have all experienced the deeply nested hierarchies of Object-Oriented Programming and asked ourselves “Is there a better way”? Well, I think C++ and as inherited from C already had a pretty good answer.&lt;/p&gt;

&lt;p&gt;For me at least the vast majority of my inheritance comes down to wanting to group many different types into one with some common members or functions across them. For example, in Liam I have a ‘Statement’ struct that is inherited by many other statement types like ‘IfStatement’, ‘ScopeStatement’, and so on. It is nice to bundle these up and refer to them as just ‘Statement’ as I may not need or care about what I am referring to.&lt;/p&gt;

&lt;p&gt;You can somewhat simulate this in C with the use of a union containing many structs and having some id of which type the union is currently.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;struct Statement {

    StatementType type;

    union {
        IfStatement if_statement;
        ScopeStatement scope_statement;
        LetStatement let_statement;
    };

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

&lt;/div&gt;



&lt;p&gt;This gives the same functionality of inheritance but there is one issue, you must always check the if of the statement. While this is not normally unsafe it can lead to problems like when not initializing the type still gives a valid value.&lt;/p&gt;

&lt;p&gt;To try and help with this I added Typed Unions to my language and here is how they work. You can see in the image below the type specifier for ‘x’ is multiple types separated by a pipe denoting and or semantic.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fn main(): void {
    let x: u64 | bool | str = "hello sailor!";
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This shows the value of x might be a number, a boolean, or a string but it is not known for sure. This can also be extended to struct types to replicate the behavior in C’s unions. But there is still one more thing and that is how to get the actual value from x in a type-safe way.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fn main(): void {
    let x: u64 | bool | str = "hello sailor!";

    if x is str as_string {
        println[str](*as_string);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All you do is a ‘is expression’, this just returns true if the value currently stored in the variable is the one we are asking, and if so it gives us a pointer to that value which in this example is the ‘as_string’ identifier. This new identifier is also scoped to the if block meaning this can be repeated for multiple types.&lt;/p&gt;

&lt;p&gt;Another area in which this is useful is error handling. I personally really love how Zig does error handling and these new Typed Unions can get us that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fn main(): void {
    if add(10, -20) is u64 n {
        print[u64](*n);
    }
}

fn add(x: i64, y: i64): u64 | str {
    let result := x + y;
    if result &amp;lt; 0 {
        return "Error negative number";
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This example is a bit more involved but you can see that the return of my add function is now either a number or a string where the string is my error message. I can then check if my function call was completed by using the is expression shown before.&lt;/p&gt;

&lt;p&gt;If you like this post or want to contribute to the language go and check it out on github &lt;a href="//www.github.com/jackdelahunt/Liam"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>programming</category>
    </item>
    <item>
      <title>Fixing C++ with a new open source project</title>
      <dc:creator>Jack Delahunt</dc:creator>
      <pubDate>Sun, 03 Jul 2022 00:31:18 +0000</pubDate>
      <link>https://dev.to/jackdelahunt/fixing-cwith-a-new-open-source-project-5hh7</link>
      <guid>https://dev.to/jackdelahunt/fixing-cwith-a-new-open-source-project-5hh7</guid>
      <description>&lt;p&gt;If you have ever written any code in C++ I first want to say sorry for all the needless pain you have been through but I am trying to make your life easier.&lt;/p&gt;

&lt;p&gt;Many people have given their opinion on C++ and why they either don't like it or why you should never use it. Personally I actually do like C++ for the most part but there are some very obvious issues that can be addressed. &lt;/p&gt;

&lt;p&gt;The first problem is how C++ compilers deal with parts of your code needing other parts and how it doesn't deal with them at all. Use before declaration and out of order declarations are normal and expected in a normal language and C++ does not and will not ever have this. You can see how old school C++ really is when you have to start thinking about header files and how they are included. These are just issues that shouldn't exist anymore.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// its 2022 this should work...
struct Bar {
   Foo foo;
};

struct Foo {};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;C++ also has an issue of trying to be every language in existence all at once. To this day I don't think a single person knows what every keyword means and most still don't know all the ways to use static. Normally I wouldn't be annoyed by this but the fact that using seven keywords in a function declaration is considered "best practice" is enough for me to bring it up.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// how Bjarne Stroustrup writes foo examples
virtual int inline long constexpr volatile unsigned const long foo() const noexcept = delete;

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

&lt;/div&gt;



&lt;p&gt;I couldn't bring up the flaws of C++ without talking about the STL. It is honestly the saddest part of the language. Want to see the data within this object when debugging? Want to have short compile times? Want to know how and when your memory is being deallocated? Well good luck because the STL fails on all of these issues.&lt;/p&gt;

&lt;p&gt;This is where my new and hopefully improved solution comes in but with some asterisks. My goal is to create a language as simple as C but with more modern features that C++ also provides but correctly this time. There is no need to complicate the language itself as we do not need extra problems to solve as developers, this is where languages such as C and Go inspired me.&lt;/p&gt;

&lt;p&gt;Some things my language has or plans of having.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Easy to use generic functions and structs&lt;/li&gt;
&lt;li&gt;Absolutely 0 exceptions&lt;/li&gt;
&lt;li&gt;Absolutely 0 constructors&lt;/li&gt;
&lt;li&gt;Absolutely 0 destructors&lt;/li&gt;
&lt;li&gt;Clear separation of static strings and mutable ones&lt;/li&gt;
&lt;li&gt;Absolutely 0 private anything&lt;/li&gt;
&lt;li&gt;Custom allocators that you can be set project wide (including stdlib)&lt;/li&gt;
&lt;li&gt;Absolutely 0 header files&lt;/li&gt;
&lt;li&gt;Declare your structs and functions in any order&lt;/li&gt;
&lt;li&gt;Auto pretty printing of any struct&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I said before there were some asterisks to all this. While C++ has a lot of issues with has a huge amount of support, every library you know is probably usable in C or C++ and throwing that all out would be a shame. That is my Liam generates C++, and in fact the standard library for Liam is all written in C++ but just made available in Liam through some features in the language. To get people to leave C++ I do need to keep it around just long enough so people can jump ship.&lt;/p&gt;

&lt;p&gt;The plan is to have already written C++ code made available in Liam without having to rewrite libraries all over again currently this is still in development but basic features are working.&lt;/p&gt;

&lt;p&gt;Here is some syntax samples of the language, everything here is working and already implemented.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import "stdlib/io.liam";

struct Person {
   name: String,
   age: u64
}

fn main(): u64 {
    let person := new Person {
        # this is not cpp new does not heap alloc
        make_string("your name"),
        25
    };

    print[String](person.name);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Please checkout and even contribute &lt;a href="https://github.com/jackdelahunt/Liam"&gt;here&lt;/a&gt;. I would love to see what the community can do for the project.&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>opensource</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
