Carbon (programming language)

Programming language designed for interoperability with C++ From Wikipedia, the free encyclopedia

Carbon is an experimental programming language designed for interoperability with C++.[2] The project is open-source and was started at Google. Google's engineer Chandler Carruth first introduced Carbon at the CppNorth conference in Toronto in July 2022. He stated that Carbon was created to be a C++ successor.[3][1][4] The language is expected to have an experimental MVP version 0.1 in late 2026 at the earliest and a production-ready version 1.0 after 2028.[5]

Quick facts Family, Designed by ...
Close

The language intends to fix several perceived shortcomings of C++[6] but otherwise provides a similar feature set. The main goals of the language are readability and "bi-directional interoperability" (which allows the user to include C++ code in the Carbon file), as opposed to using a new language like Rust, that, whilst being influenced by C++, is not two-way compatible with C++ programs. Changes to the language will be decided by the Carbon leads.[7][8][9][10] It aims to build on top of the C++ ecosystem the way in an analogous role to TypeScript to JavaScript, or Kotlin to Java.[2]

Carbon's documents, design, implementation, and related tools are hosted on GitHub under the Apache-2.0 license with LLVM Exceptions.[11]

Example

The following shows how a program might be written in Carbon and C++:[12]

More information C++ ...
CarbonC++
package Geometry;
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 converts `Array` to `Slice`.
    PrintTotalArea(circles);
    return 0;
}
import std;

using std::span;
using std::vector;

struct Circle {
    float r;
};

void PrintTotalArea(span<Circle> circles) {
    float area = 0.0f;
    for (const Circle& c : circles) {
        area += std::numbers::pi * c.r * c.r;
    }
    std::println("Total area: {}", area);
}

int main() {
    vector<Circle> circles{{.r = 1.0f}, {.r = 2.0f}};
    // Implicitly converts `vector` to `span`.

    PrintTotalArea(circles);
    return 0;
}
Close

See also

References

Related Articles

Wikiwand AI