Is it here to stay? Will it take hold?

In a myriad of programming languages, do we really need another one? According to Google, the answer is yes.

Officially unveiled to the world a week ago by Chandler Carruth at CppNorth, Carbon has got the programming communities talking.

But what is it? How does it work? and does it work?

C++ works, but what’s the problem?

C++ has been around the block for much longer than some of us have been alive. Developed in 1982 and released in 1985, C++ has found its way into operating systems, browsers, and games.

While C++ is not the coolest kid to learn (unless you want to go down the game dev track), but it still holds a strong foothold for applications that requires performance, speed, and is a bit strapped with resource availability.

In a nutshell, C++ is a general-purpose programming language that has all the usual bells and whistles such as classes and objects, abstraction, encapsulation, polymorphism, and inheritance. It’s strongly typed, case sensitive, uses pointers, and has a massive functions library.

So, what’s wrong with C++ ?

The general criticism of C++ is that it leans towards being overly complex. IntelliSense generally sucks, no support for first-class functions and tuples, and initializer lists are considered a ‘hack’. In addition to this, there are a few quirks like duplicate syntax and operators such as the & being both a logical operator and a reference.

Then there’s the issue of each compiler vendor making up their own names and prevents linking modules from different compilers.

There’s a bag full of other problems but in short, C++ works but it has its issues.

What Carbon solves

Like every programming language made before many of us existed, it made sense at the time of its inception — but not anymore.

As software engineers, programmers, and developers become more versed in the art of translating ideas into logic, we find better ways to manifest them into code.

Go, Swift, Kotlin and Rust are all derivatives of their associated programming language. They were all created with the aim to simplify the complex. Carbon is on the same mission.

According to the official GitHub repo, Carbon is a successor language. This means that it doesn’t aim to improve C++ but replace it completely in due time.

It has similar approaches to C++ such as performance matching but with an easier learning curve to make it more accessible to a larger group of developers.

The approach is similar to Kotlin and TypeScript, where you don’t have to get rid of your existing codebase, and just have Carbon interop with the C++ project and make things work.

Here’s a code snippet of Carbon working with C++ :

// C++ code used in both Carbon and C++:
struct Circle {
 float r;
};

// Carbon exposing a function for C++:
package Geometry api;
import Cpp library "circle.h";
import Math;

fn PrintTotalArea(circles: Slice(Cpp.Circle)) {
 var area: f32 = 0;
 for (c: Cpp.Circle in circles) {
   area += Math.Pi * c.r * c.r;
 }
 Print("Total area: {0}", area);
}

// C++ calling Carbon:
#include <vector>
#include "circle.h"
#include "geometry.carbon.h"

auto main(int argc, char** argv) -> int {
 std::vector<Circle> circles = {{1.0}, {2.0}};
 // Carbon's `Slice` supports implicit construction from `std::vector`,
 // similar to `std::span`.
 Geometry::PrintTotalArea(circles);
 return 0;
}

(Source: Official Carbon snippets)

Cool. So we’ve got a C++ upgrade equivalent but what are the actual perks?

  • generics: in C++, generics are only generics until the types get substituted at runtime. Templates solve this but it isn’t a final solution. Carbon does generics properly by parameterizing the code rather than making near-duplicates like how it’s currently working in templates.
  • inheritance: C++ is abstract base classes and don’t have data members. Carbon offers non-polymorphic inheritance.
  • operator overloading: you can define how Carbon operators are applied in custom types.

That’s the general gist of it. Other perks include faster and increased scalability in development through better code organization and modular structures. Another major perk of Carbon is that it has all the learnings and pitfalls experienced in C++ , something the predecessor never had due to the environment that it was created in.

So, when’s the full release?

Currently, Carbon is in an experimental phase. The current roadmap is as follows:

  • release of a core working version (0.1) by end of 2022
  • 0.2 in 2023
  • Full 1.0 release in 2024–2025

That’s basically it for now. The documentation for Carbon is generally succinct and accessible — even to those who are not C++ developers.

Overall, it should be interesting how Google pushes Carbon once it’s fully ready. Will it be linked to all the metaverse stuff that’s up and coming everywhere? (Sort of like how Kotlin got pushed through Android development to replace Java) Or perhaps it will be linked to Android-based game development?

Whatever the future, Carbon is coming. How it plays out, we will probably find out in a few years’ time.

Share this post