Since the dawn of time C and C++ have been the de facto languages of choice when building embedded systems, mission critical software, and applications with extreme power or performance requirements. In fact, C++ was my first love and the programming language that got me out of math class in high school.

At the CPP North C++ conference in Toronto, on July 19, 2022, Google engineer Chandler Carruth announced Carbon. Please watch Chandler’s full talk on YouTube – I’ll do my best to give you an abbreviated run through.

What does Carbon offer?

Carbon is not meant to be a replacement for C++, instead Carbon’s primary goal is to be a successor to C++ the same way that Swift succeeded Objective-C. The way that Carbon plans to do this is by supporting interoperability. That’s just a fancy way to say that Carbon can run within C++ and C++ can run directly in Carbon. Let’s go over the bullet points:

  • Interoperability
  • Updated Grammar
  • Tool-based upgrades
  • “Batteries Included” approach

Carbon and C++ Interoperability

The decision for interoperability is significant for Carbon because it allows the team to avoid taking all of the technical debt from C++. Most of the technical debt in C++ is due to the requirement of backwards compatibility.

// Circle.h
struct Circle {
  float r;
};
#include <vector>
#include "circle.h"

// Include Carbon package, as module
#include "geometry.carbon.h"

auto main(int argc, char** argv) -> int {
  std::vector<Circle> circles = {{1.0}, {2.0}};

  // Carbon running in C++!
  Geometry::PrintArea(circles);
  return 0;
}
package Geometry api;
import Math;

// include cpp header!
import Cpp library "circle.h";

fn PrintArea(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);
}

Updated Grammar

I can’t cover all of the changes in this article, but some of the notable changes to the grammar are: varlet, read-only function input parameters, and function naming conventions.

The most exciting of this list is the inclusion of read-only function input parameters. By doing this, Carbon is able to allow access to the input values without creating a copy of the original object. This is a huge optimization for developers and will no doubt save both new and old C++ developers from wasting memory.

Additionally, they have chosen to make functions public by default. This is an awkward thing to do since my first instinct is to have private functions and then explicitly allow access to other functions, the choice here was purely aesthetic – by dropping the public/private qualifier the function is easier for clients of the API to read.

Tool-based Upgrades

Personally, I really like the approach of tool-based upgrades, but there are some huge problems with it. When a new version of Carbon is released and there are breaking changes, developers will need to grab the latest tooling and run their outdated files through it – easy! While that might be the case for small projects, it sounds like a nightmare for enterprise sized projects. 

By using tool-based upgrades, Carbon has to assume that developers are following best-practices and writing clean enough code for a tool to update. Speaking from experience, that’s not usually the case. Instead, all of the syntax will get changed but the logical dependencies between functions may not translate so well.

“Batteries Included” approach

The Carbon team has a lofty goal to supply documentation, tooling, compilers, and any other tech needed to bootstrap Carbon development as a single package. If they are able to make this a reality, it will be super helpful for developers but also a huge benefit to help them onboard new enthusiasts. 

As a stretch goal, the Carbon team also wants to have a package manager – like NPM. 

Summary

If Carbon is the next TypeScript or Swift, but for C++, sign me up! Although C++ was my first programming language, it’s far from friendly when you consider the landscape. During the Q&A I did not hear anyone explicitly mention it, but, I’m hopeful that Carbon is available for enthusiast microcomputers like the Raspberry Pi and Arduino families. I’m excited for Carbon and I really hope that with the initial support from Google, that Carbon is here to stay.

As an experimental project, there’s isn’t a ton of Carbon documentation available, but, I’ll provide what I can find in the list below