Carbon (programming language): Difference between revisions

Content deleted Content added
No edit summary
m rv IP 1 April 2024
 
(41 intermediate revisions by 31 users not shown)
Line 1:
{{Short description|General-purpose programmingProgramming language designed for interoperability with C++}}
 
{{Use dmy dates|date=July 2022}}
Line 10:
| designer = [[Google]]
| typing = [[Static type|Static]], [[Nominal type system|nominative]], [[Type inference|partly inferred]]
| influenced by = [[C++]], [[Rust (programming language)|Rust]], [[GoZig (programming language)|GoZig]], [[Haskell (programming language)|Haskell]], [[Kotlin (programming language)|Kotlin]], [[Swift (programming language)|Swift]]<ref name="cppnorthkeynote-video" />
| programming language = [[C++]]
| license = [[Apache-2.0-with-LLVM-Exception]]
Line 17:
}}
 
'''Carbon''', or Carbon-Lang, is an experimental, [[general-purpose programming language]]. Thedesigned projectfor isinteroperability with [[Open-sourceC++]].<ref softwarename="readme">{{cite web|opentitle=README|url=https://github.com/carbon-source]]language/carbon-lang/blob/trunk/README.md|quote="It andis wasdesigned startedaround byinteroperability [[Google]],with followingC++ inas thewell footstepsas oflarge-scale previousadoption Googleand migration for existing C++ codebases and developers."|accessdate=2023-made09-06}}</ref> programmingThe languagesproject is ([[GoOpen-source (programming language)software|Goopen-source]] and [[Dartwas (programmingstarted language)|Dartat [[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.<ref name="cppnorthkeynote-sched" /><ref name="cppnorthkeynote-video" /><ref name="cppnorthkeynote-9to5google" /> The language is expected to have aan 1.0experimental release[[Minimum viable product|MVP]] version 0.1 in 2024late 2026 at the earliest and a production-ready version 1.0 orafter 20252028.<ref>{{Citation |title=Carbon Language: An experimental successor to C++Roadmap |date=20222024-1001-1211 |url=https://github.com/carbon-language/carbon-lang/blob/9feff92f223e1a855abf2b343387d8460f63317btrunk/docs/project/roadmap.md |publisher=carbon-language |access-date=20222024-1001-1218}}</ref>
 
The language intends to fix several perceived shortcomings of C++<ref name="difficulties improving cpp" /> 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 (programming language)|Rust]], that, whilewhilst being influenced by C++, is not two-way compatible with C++ programs. Changes to the language will be decided by the Carbon leads.<ref name="c1" /><ref name="c2" /><ref name="c3" /><ref name="c4" />
 
Carbon's documents, design, implementation, and related tools are hosted on [[GitHub]] under the [[Apache License|Apache-2.0]] license with [[LLVM]] ExceptionExceptions.<ref>{{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}}</ref>
 
==Example==
The following shows how a program might be written in Carbon and C++:<ref>{{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}}</ref>
The following shows how a [["Hello, World!" program]] is written in Carbon:
{{clear}}
<!--
Part of the Carbon Language project, under the Apache License v2.0 with LLVM
Exceptions. See https://github.com/carbon-language/carbon-lang/blob/9a7b7c2b8be3bcf288d29eb9b7afbf37fb471c1b/LICENSE for license information.
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Source: https://github.com/carbon-language/carbon-lang/blob/9a7b7c2b8be3bcf288d29eb9b7afbf37fb471c1b/docs/images/snippets.md
-->
{| class="wikitable"
|-
! Carbon !! C++
|- style=vertical-align:top
| <syntaxhighlight lang="carbon" line="1">
package Sample apiGeometry;
import Math;
 
class Circle {
<syntaxhighlight lang="carbon" line="1">
var r: f32;
package Sample api;
}
 
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 s: auto = "Hello, World!";
var circles: Array(Circle) = ({.r = 1.0}, {.r = 2.0});
Print(s);
// Implicitly converts `Array` to `Slice`.
return 0;
PrintTotalArea(circles);
return 0;
}
</syntaxhighlight>
| <syntaxhighlight lang="c++" line="1">
The following is the equivalent "Hello, World!" program written in C++:
#include <iostreamvector>
<syntaxhighlight lang="c++" line="1">
using namespace std;
#include <iostream>
 
struct Circle {
float32_t r;
};
 
void PrintTotalArea(span<Circle> circles)
{
float32_t area = 0;
for (const Circle& c : circles) {
area += numbers::pi * c.r * c.r;
}
print("Total area: {}\n", area);
}
 
int main() {
vector<Circle> circles{{.r = 1.0}, {.r = 2.0}};
auto s = "Hello, World!";
// Implicitly converts `vector` to `span`.
std::cout << s;
 
return 0;
PrintTotalArea(circles);
return 0;
}
</syntaxhighlight>
|}
 
 
==See also==
Line 54 ⟶ 94:
* [[C++]]
* [[D (programming language)|D]]
* [[Go (programming language)|Go]]
* [[Rust (programming language)|Rust]]
* [[Mojo (programming language)|Mojo]]
 
== References ==
Line 61 ⟶ 103:
|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-2017–20, 2022
|publisher=CppNorth
|via=Sched.com
Line 133 ⟶ 175:
== External links ==
* {{GitHub|carbon-language}}
* [https://docs.carbon-lang.dev/ Carbon language documentation]
* [https://carbon.godbolt.org/ Carbon at the Compiler Explorer (godbolt)]
 
{{Google LLCFOSS}}
{{Google-stub LLC}}
 
[[Category:Google]]
Line 140 ⟶ 186:
[[Category:Cross-platform software]]
[[Category:Object-oriented programming languages]]
[[Category:Programming languages created in 2022]]
 
{{[[Category:Google FOSS}}software]]
{{Google LLC}}
 
{{Google-stub}}