Heya! We use cookies

Codesphere uses cookies to understand your preferences and make sure you have the best experience on our site. By using Codesphere, you accept our use of cookies.

Understood. Yum!

Google’s new experimental programming language: Carbon

14.09.2022

Carbon also known as Carbon-lang was launched in July, 2022 as an experimental open-source successor to C++. The project was started by Google and is currently open for contributions on GitHub.

The core goal behind the language is to cover some of the shortcomings of C++ which is used by about 4.4 million developers worldwide. Naturally whenever a product has a problem the easiest and fastest way to go about would be to incrementally improve the already existing product, but why did Google choose to start a completely new language instead?

The reason behind this is that it is quite challenging and hard to bring new features or changes to a product like C++ even for a company like Google. This is due to the attention to backward compatibility, the strict governance behind the language as well as a lengthy approval process that can take up to years. You can read more on the challenges faced in carbon documentation here.

This is similar to how TypeScript was created to cover the shortcomings of Javascript or Kotlin for Java.

Carbon is being designed to create performance-critical software while supporting software and language evolution. Another core goal is to make code that easy to read, understand and write. This enables fast and scalable development for Modern OS platforms, hardware architectures, and environments. Last but not least, carbon also supports interoperability with and migration from existing C++ code.

Apart from that Carbon has a few key features which we will look at in detail.

Fast Performance: Like its predecessor (C++), carbon is meant to have performance advantages using LLVM or Low-level virtual machine; having access to bits and addresses.

Bi-directional Interoperability: Meaning that you can call C++ language code from Carbon and vice versa.

Migration: Provides Support to migrate code from C++ to carbon.
Fast and Scalable Builds: As mentioned before this enables developers to

make fast and scalable builds that can work with existing c++ builds too.

Built with Open Source: The whole language is publicly accessible through GitHub where users can contribute to the program according to the set goals and priorities.

Another question you might have at this point is whether C++ has any other languages which are set to replace it somewhere down the line, the answer is yes! One example is Rust which is thoroughly evaluated as a replacement for C++. At the moment Rust is a more viable language and even the creators of Carbon itself acknowledge this. However, with constant development and improvements being brought to Carbon, there is a high chance that this will change at some point in the future.

Carbon is relatively very new and is definitely at an experimental stage. This means that there is no working compiler at the moment. However, there are methods through which you can take Carbon for a spin, which we will be looking at now!

As prerequisites to install Carbon you will need the following tools and frameworks to be installed on your device.

homebrew : A package installation tool.
bazel : Open source build and test tool.
LLVM : Low-Level virtual machine that carbon language will run on. Carbon Explorer : The implementation tool we will be using for carbon.

Step 1: Install Bazel

To do this go to the command prompt of your device and type in the following command.

$ brew install bazelisk

Step 2: Install LLVM

This is the virtual machine on which carbon will run. To install this once again proceed to the terminal of your device and run the following command.

$ brew install llvm

$ export PATH="$(brew --prefix llvm)/bin:${PATH}"

Step 3: Setup Carbon Language!

This will download the carbon language code along with a set of pre curate examples.

$ git clone https://github.com/carbon-language/carbon-lang

$ cd carbon-lang

Time to try it for yourself!

As always there is no better code to run than a ‘Hello world’ program to start with! To do ths proceed to the carbon-lang directory and run the below command.

$ bazel run //explorer -- ./explorer/testdata/print/format_only.carbon

This will generate a format_only.carbon file in the ./explorer/testdata/print directory which carries the instruction to execute the code. Once this file is run you should be able to see your output similiar to whats show below!

tipseason$ bazel run //explorer -- ./explorer/testdata/print/format_only.carbon
INFO: Invocation ID: aa36a266-cb55-4433-9bed-907e393c0605
WARNING: .....
.
.
INFO: Build completed successfully, 390 total actions
.
.
Hello world!
result: 0

And we have succesfully created a program with carbon. For more information on examples, detailed documentation and to contribute to carbon proceed to GitHub repository here.

That’s all for today!

Got any questions? Let us know down below.

As always, happy coding from your friends at Codesphere, the swiss army knife that every development team needs!