Will Carbon replace C++?
Tags: programming, discuss, news, cpp
Published: 2022-07-26 12:03:53

If you are into C++, here is a piece of fresh news for you!

Google developers have released a brand-new "experimental" open source language named Carbon.

Here is a complete tutorial: https://betterprogramming.pub/carbon-programming-language-tutorial-6d67b4cc16ae


![Carbon Programming Language](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s0h16xjzho9i792cntxu.png)



## Successor to C++

Carbon could serve as a successor language to C++. 

One that provides a simple starting point for developers to a newer language that addresses contemporary development concepts like memory safety and generics. 

The conditional is a must as the documentation itself says that Carbon is "An experimental successor to C++".

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/80td78a2ed0wodmrmvek.png)

If you prefer watching  the video and reading the material I used, here are some links:

- watch the 1.5h long video on [YouTube](https://www.youtube.com/watch?v=omrY53kbVoA&ab_channel=CppNorth), 
- read the [documentation on GitHub](https://github.com/carbon-language/carbon-lang)

## What Are The Promises Of Carbon?
Starting from the difficulties experienced with C++, Carbon adopts a different approach. 

### 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++", 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. 
- contributing is 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, IDE tools), and tool-based upgrades
- bridging a gap in the C++ ecosystem with a built-in package manager.

## Carbon examples and demo

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);
}
```

### Carbon Explorer
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".

Here is a link to Carbon Explorer.

If you want to know more about why C++, Rust, and other languages are not enough read a more extensive [article on Medium](https://levelup.gitconnected.com/googles-carbon-might-replace-c-7b634b465f51).

## Developers' sentiment 
This is a collection of developers' feelings, posts, tweets, comments, etc.
- 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 controversial. 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."

## Conclusions
This article wants to be an overview of the Carbon language. 

It is still very early to judge and I am in no position to do that. However, it is super exciting to see a new project starting from scratch and moving the first steps.
 
If you are interested, you could contribute and be part of something that might become very big in a few years.

What are your thoughts?
### Are you optimistic or skeptical of Carbon?