DEV Community

Alex Spinov
Alex Spinov

Posted on

Carbon Has a Free API: Google's Experimental Successor to C++

Carbon is Google's experimental programming language designed as an evolution of C++ — with modern syntax, memory safety features, and seamless C++ interop.

Why Carbon Matters

C++ is used in billions of lines of production code but is notoriously difficult to modernize. Carbon provides a migration path: use Carbon alongside existing C++ code, gradually replacing it.

What you get for free:

  • Modern syntax that's easier to read and write than C++
  • Seamless C++ interoperability (call C++ from Carbon and vice versa)
  • Generics with checked definitions (no SFINAE)
  • Pattern matching and tagged unions
  • Memory safety improvements (planned for v1.0)
  • Built on LLVM for production-grade performance

Quick Start

# Clone Carbon
git clone https://github.com/carbon-language/carbon-lang
cd carbon-lang

# Build the explorer (interpreter)
bazel build //explorer

# Run a Carbon program
bazel run //explorer -- ./examples/hello.carbon
Enter fullscreen mode Exit fullscreen mode

The Basics

package Sample api;

fn Main() -> i32 {
  var name: String = "Carbon";
  Print("Hello from {0}!", name);

  var x: i32 = 42;
  var y: auto = x * 2;  // Type inference
  Print("x = {0}, y = {1}", x, y);

  return 0;
}
Enter fullscreen mode Exit fullscreen mode

Functions and Types

package Geometry api;

// Classes
class Point {
  var x: f64;
  var y: f64;

  fn Distance[self: Self](other: Point) -> f64 {
    var dx: f64 = self.x - other.x;
    var dy: f64 = self.y - other.y;
    return Math.Sqrt(dx * dx + dy * dy);
  }
}

// Choice types (tagged unions)
choice Shape {
  Circle(f64),
  Rectangle(f64, f64),
  Triangle(f64, f64, f64)
}

fn Area(shape: Shape) -> f64 {
  match (shape) {
    case .Circle(r: auto) => return 3.14159 * r * r;
    case .Rectangle(w: auto, h: auto) => return w * h;
    case .Triangle(a: auto, b: auto, c: auto) => {
      var s: f64 = (a + b + c) / 2.0;
      return Math.Sqrt(s * (s - a) * (s - b) * (s - c));
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Generics (Checked)

package Generics api;

// Interface (like C++ concepts, but checked)
interface Printable {
  fn Print[self: Self]();
}

interface Comparable {
  fn Compare[self: Self](other: Self) -> i32;
}

// Generic function — compiler checks ALL constraints at definition time
fn FindMax[T:! Comparable](items: Array(T)) -> T {
  var max: T = items[0];
  var i: i32 = 1;
  while (i < items.size()) {
    if (items[i].Compare(max) > 0) {
      max = items[i];
    }
    ++i;
  }
  return max;
}

// Implementation
impl i32 as Comparable {
  fn Compare[self: Self](other: Self) -> i32 {
    return self - other;
  }
}
Enter fullscreen mode Exit fullscreen mode

C++ Interop

package MyCarbonLib api;

// Import C++ headers directly
import Cpp library "my_cpp_lib.h";

fn ProcessData(data: Cpp.std.vector(i32)) -> i32 {
  // Call C++ functions directly
  var result: i32 = Cpp.myFunction(data);
  return result;
}
Enter fullscreen mode Exit fullscreen mode
// C++ can call Carbon just as easily
#include "my_carbon_lib.carbon.h"

int main() {
  auto result = Carbon::MyCarbonLib::ProcessData(myData);
  return result;
}
Enter fullscreen mode Exit fullscreen mode

Carbon vs C++ Syntax

Feature C++ Carbon
Function int foo(int x) fn Foo(x: i32) -> i32
Variable int x = 5; var x: i32 = 5;
Class class Foo { ... }; class Foo { ... }
Generic template<typename T> fn Foo[T:! Interface]()
Enum enum class choice
Pointer int* i32*

Useful Links


Building high-performance systems? Check out my developer tools on Apify for ready-made web scrapers, or email spinov001@gmail.com for custom solutions.

Top comments (0)