Carbon (programming language)#Example

{{Short description|Programming language designed for interoperability with C++}}

{{Use dmy dates|date=July 2022}}

{{Infobox programming language

| name = Carbon

| logo = Carbon logo.png

| logo alt = A dark-gray circle with a white sans-serif letter "C" in the middle

| logo caption = Logo on Carbon's GitHub organization

| family = C

| designer = Google

| typing = Static, nominative, partly inferred

| influenced by = C++, Rust, Zig, Haskell, Kotlin, Swift

| programming language = C++

| license = Apache-2.0-with-LLVM-Exception

| file ext = .carbon

| website = {{URL|github.com/carbon-language}}

}}

Carbon is an experimental programming language designed for connectiveness with C++.{{cite web|title=README|url=https://github.com/carbon-language/carbon-lang/blob/trunk/README.md|quote="It is designed around interoperability with C++ as well as large-scale adoption and migration for existing C++ codebases and developers."|accessdate=2023-09-06}} The project is open-source and was started at Google. Google 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. 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.{{Citation |title=Carbon Language: Roadmap |date=2024-01-11 |url=https://github.com/carbon-language/carbon-lang/blob/trunk/docs/project/roadmap.md |publisher=carbon-language |access-date=2024-01-18}}

The language intends to fix several perceived shortcomings of C++ 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.

Carbon's documents, design, implementation, and related tools are hosted on GitHub under the Apache-2.0 license with LLVM Exceptions.{{Cite web |url=https://github.com/carbon-language/carbon-lang/blob/31df852738aea520a1a1800259120bc10ce7a005/LICENSE |title=carbon-lang/LICENSE |date=2020-06-16 |access-date=2022-07-24 |website=GitHub}}

Example

The following shows how a program might be written in Carbon and C++:{{cite web |title=carbon-lang/docs/images/snippets.md at trunk · carbon-language/carbon-lang |url=https://github.com/carbon-language/carbon-lang/blob/trunk/docs/images/snippets.md |website=GitHub |access-date=16 December 2023 |language=en}}

{{clear}}

class="wikitable"
CarbonC++
style=vertical-align:top

|

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;

struct Circle {

std::float32_t r;

};

void PrintTotalArea(std::span circles) {

std::float32_t area = 0;

for (const Circle& c : circles) {

area += std::numbers::pi * c.r * c.r;

}

std::print("Total area: {}\n", area);

}

int main() {

std::vector circles{{.r = 1.0}, {.r = 2.0}};

// Implicitly converts `vector` to `span`.

PrintTotalArea(circles);

return 0;

}

See also

References

{{Reflist|refs=

{{Cite web

|url=https://cppnorth2022.sched.com/event/140f8/keynote-chandler-carruth-nulbscience-experiment-timenulb?linkback=grid

|title=Scheduled events for Tuesday, July 19, 09:00 - 10:30

|work=CppNorth, The Canadian C++ Conference, July 17–20, 2022

|publisher=CppNorth

|via=Sched.com

|access-date=2022-07-21

}}

{{Cite web

|url=https://www.youtube.com/watch?v=omrY53kbVoA

|title=Carbon Language: An experimental successor to C++ - Chandler Carruth - CppNorth 2022

|publisher=CppNorth

|via=YouTube

|date=2022-07-22

}}

{{cite web

|url=https://9to5google.com/2022/07/19/carbon-programming-language-google-cpp/

|last1=Bradshaw

|first1=Kyle

|title=Carbon, a new programming language from Google, aims to be C++ successor

|work=9to5Google

|date=19 July 2022

}}

{{Cite web

|title=Difficulties improving C++

|url=https://github.com/carbon-language/carbon-lang/blob/b62b7464a4f99f9101edbe3ea5b76d6cb2cdbc9b/docs/project/difficulties_improving_cpp.md

|work=carbon-language/carbon-lang repo

|publisher=Google

|via=GitHub

|date=2022-07-21

}}

{{cite web

|url=https://github.com/carbon-language/carbon-lang/blob/trunk/docs/project/evolution.md

|first1=Chandler

|last1=Carruth

|first2=Jon

|last2=Ross-Perkins

|first3=Matthew

|last3=Riley

|first4=Sidney

|last4=Hummert

|title=Evolution and governance

|work=carbon-language/carbon-lang repo

|publisher=Google

|via=GitHub

|date=23 July 2022

}}

{{Cite web

|url=https://mybroadband.co.za/news/software/453410-googles-carbon-programming-language-aims-to-replace-c.html

|last=Illidge

|first=Myles

|title=Google’s Carbon programming language aims to replace C++

|work=MyBroadband

|date=21 July 2022

}}

{{Cite web

|title=Google Launches Carbon, an Experimental Replacement for C++

|url=https://thenewstack.io/google-launches-carbon-an-experimental-replacement-for-c/

|last=Jackson

|first=Joab

|date=20 July 2022

|work=The New Stack

}}

{{Cite web

|title=Carbon, A New Programming Language from Google As A C++ Successor

|url=https://www.phoneworld.com.pk/carbon-a-new-programming-language-from-google-as-a-c-successor/

|last=Mustafa

|first=Onsa

|date=20 July 2022

|work=PhoneWorld

}}

}}