It’s really happening. Google developers have released a brand-new, experimental, open-sourced language named Carbon.
Carbon could serve as a successor language to C++, and it’s one that provides a simple starting point for developers to a newer language that addresses contemporary development concepts like memory safety and generics.
This would be similar to how Microsoft built Typescript to update JavaScript and Kotlin to strengthen weaknesses in Java.
Google developer Chandler Carruth unveiled the language in July 2022 at the CPP North conference in Toronto by Google developer Chandler Carruth.
I’ll summarize a few points from the conference and add some flavor from other resources, comments, tweets and documentation, etc.
What’s Wrong With C++?
In the words of Carruth in his Carbon documentation on Github:
“C++ is the dominant programming language for the performance critical software our goals prioritize. The most direct way to deliver a modern and excellent developer experience for those use cases and developers would be to improve C++.
Improving C++ to deliver the kind of experience developers expect from a programming language today is difficult.”
Why Is It Difficult To Improve C++?
Carruth mentioned a few points that can be summarized in two areas: language and bureaucracy
Language
- Technical debt: Necessary choices that made C++ great are becoming a burden. C++ accumulated decades of technical debt.
- Prioritization of backward compatibility: More and more features have been added rather than removed or replaced. While backward compatibility is important, it comes at a cost. It adds and prevents fixing the technical debt.
Governance
- Processes: The process to improve C++ goes through a bureaucratic committee approach that prioritizes standardization above design.
- Limited access: Access to the committee and standard is restricted and expensive.
- Interests of the few: While some nations and companies are represented, many other stakeholders are not.
- Lengthy decision process: Decisions can take years, or not reach any definitive conclusion.
All told, it’s difficult to improve C++.
Having said that, other garbage-collected languages were considered but didn’t meet the performance required by specific users and organizations.
What Is Carbon?
As stated in the goals, “Carbon is an experiment to explore a possible, distant future for the C++ programming language designed around a specific set of goals, priorities and use cases.”
Syntax
Among the presented features, it is worth mentioning:
- Introducer keywords:
fn
for function,var
for variable declarations. - Function input parameters are read-only values.
- Pointers provide indirect access and mutation.
- Expressions to name type.
- The namespace at the root is always local.
- Public members by default. The reasoning seems to be that since you will mostly read the public functions in your API, it makes sense to expose them.
- Type checking generics.
This list is by no means complete, but it gives an overview of some characteristics of the language.
Below you can see a snippet from the example code on GitHub, with some simple comments from my side.
// Carbon:
package Geometry api; // local namespace
import Math; // library import
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);
}
Is Carbon a Successor Language for C++?
The team behind Carbon spent some time considering how other languages could tackle these issues.
Rust might seem a great solution, and the recommendation is clear. If Rust works for you today, you should use it, but moving a C++ ecosystem to Rust is hard.
Carbon aims to replace large C++ codebases that are difficult to convert into Rust.
Furthermore, Carruth observed how each of them was designed for a purpose and introduced the concept of successor languages.
According to Carruth, successor languages are:
- Built on an existing ecosystem without bootstrapping a new one.
- Provide bi-directional interoperability.
- Optimize the learning curve and adoption path.
- Ideally have tool-assisted migration support.
Given the context, it seems reasonable to think of a new purpose-driven language that builds on the six goals for C++ and adds one more:
- Performance-critical software.
- Software and language evolution.
- Code that is easy to read, understand and write.
- Practical safety and testing mechanisms.
- Fast and scalable development.
- Modern OS platforms, hardware architectures and environments.
- Interoperability with and migration from existing C++ code.
What Are the Advantages of Carbon?
Starting from the difficulties experienced in the language and in the governance, Carbon adopts a different approach for both areas.
Language
Carbon wants to start from scratch including:
- Modern generics system.
- Modular code organization.
- Simple syntax.
Carbon wants to be “a successor language […], rather than an attempt to incrementally evolve C++,” according to Carruth’s post on carbon-lang.
For this reason, it gave up on transparent backward compatibility while remaining interoperable with and migratable from C++.
Governance
Carbon wants to be more inclusive by:
- Building on open-source principles, processes and tools. This makes contributing easier and more transparent.
- Having a clear governance structure that can make decisions rapidly when needed.
- Expanding the ecosystem with tools that provide a rich developer experience (compiler, standard library and IDE tools) and tool-based upgrades.
- Bridging a gap in the C++ ecosystem with a built-in package manager.
How Will Carbon Be Governed?
Carruth expanded on Carbon’s governance while talking about culture and community. This includes:
- An inclusive and welcoming community, with a comprehensive code of conduct.
- A friendly and approachable community.
- Open process for governance and changes.
- Decisions with clear rationale, especially when a difficult trade off.
Though Google first founded Carbon, the aim is to reduce the contributions from any single company to less than 50 percent by the year’s end.
Eventually, they hope to transfer ownership of the project to a nonprofit software foundation, where it will be developed by volunteers.
How Has Carbon Been Received?
As Carbon has rolled out, developers have shared their feelings via posts, tweets and comments:
- Developers seem to be mostly optimistic about Carbon
- Most love the interoperability with C++.
- There is some skepticism around Carbon replacing C++. Some argue that Carbon will excel in a niche but won’t become widely adopted.
- Immutable declarations are preponed by let, which might be confusing for developers coming from JavaScript. However, this will be smooth for devs coming from Swift.
- Public members by default seems to be a controversial decision. The reasoning seems to be that since you will mostly read the public functions in your API, it makes sense to expose them.
- Rust supporters have been addressed: “If Rust works for you today, you should use it. But moving a C++ ecosystem to Rust is hard.”
It’s still too early to evaluate Carbon, but it’s exciting to see a new project starting from scratch and making its first steps.
If you are interested, you could contribute and be part of something that might become very big in a few years.
Explore Carbon Programming Language
Fancy trying it out?
Carbon Explorer is “A prototype interpreter demo that can both run isolated examples and gives a detailed analysis of the specific semantic model and abstract machine of Carbon,” according to its documentation.
References:
- Watch the Carbon presentation.
- Read “Difficulties Improving C++,” on GitHub.
- Read “Carbon Language: An experimental successor to C++,” on GitHub.
- Read the “Project FAQ,” on GitHub.