Google engineer Chandler Carruth recently announced at the CppNorth conference in Toronto, officially open-sourcing Google’s internally built programming language: Carbon. He also called Carbon the successor to C++ (which is currently in the experimental stage).

Carbon

Carbon

Chandler cites examples of the evolution of today’s popular programming languages, such as Java to Kotlin, Objective-C to Swift, JavaScript to TypeScript, and C++, which is widely used within Google and is seen as somewhat of a successor to C. He believes these successors can help developers quickly become productive and take advantage of modern language features.

Chandler also mentions Rust, which is also initially billed as a successor to C++, but the relationship is not as “bi-directional interoperability” as Java and Kotlin, making it difficult to migrate consistently. As for Carbon, it shares many of the same goals as Rust and supports full interoperability with existing C++ code, with the goal of making it as easy as possible for developers to migrate from C++ to Carbon.

Carbon Highlights.

  • Introducer keyword and simple syntax
  • Function input parameters are read-only values
  • Pointer provides indirect access and variants
  • Use expressions to name types
  • Package as root namespace
  • API import by package name
  • Method declaration with explicit object arguments
  • Single inheritance, default use of final class
  • Powerful and definition-checked generics
  • Types can explicitly implement interfaces

C++ code versus Carbon code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// C++:
#include <math.h>
#include <iostream>
#include <span>
#include <vector>

struct Circle {
  float r;
};

void PrintTotalArea(std::span<Circle> circles) {
  float area = 0;
  for (const Circle& c : circles) {
    area += M_PI * c.r * c.r;
  }
  std::cout << "Total area: " << area << "\n";
}

auto main(int argc, char** argv) -> int {
  std::vector<Circle> circles = {{1.0}, {2.0}};
  // Implicitly constructors `span` from `vector`.
  PrintTotalArea(circles);
  return 0;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Carbon:
package Geometry api;
import Math;

class Circle {
  var r: f32;
}

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

fn Main() -> i32 {
  // A dynamically sized array, like `std::vector`.
  var circles: Array(Circle) = ({.r = 1.0}, {.r = 2.0});
  // Implicitly constructs `Slice` from `Array`.
  PrintTotalArea(circles);
  return 0;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 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;
}

The code for the Carbon language is currently fully open source. Chandler says that while Carbon was born from within Google and its current project leaders are largely (not entirely) made up of Google employees, it aims to be an “independent and community-driven open source project.”

If you are interested in Carbon, you can download the source code and experiment with it on your own device, or experience the Carbon programming language directly in your browser via Compiler Explorer to experience the Carbon programming language directly in your browser.

Some background information on the Carbon project was revealed by a developer: In February 2020, the C++ standards committee voted on a proposal to “break ABI compatibility for performance”, an effort largely driven by Google employees, but the vote ultimately failed. As a result, many Google employees have stopped participating in C++ standardization efforts, resigned their official positions on the committee, and clang development has slowed considerably. Based on this background and the goals Google has set for Carbon, this developer believes that Google really wants to make Carbon a replacement for C++.