Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 25, 2022 02:08 am GMT

Google's Carbon Language: An Alternative to C ?

Programming languages that were popular a year or even a decade ago are now slowly becoming obsolete. For instance, Microsoft eliminated JavaScript with Typescript, Apple eliminated Objective C with Swift, JetBrains eliminated Java with Kotlin, and now Google is stepping up to take on the greatest challenge of them all, eliminating the almighty C++ with Carbon.

Difference

Google recently unveiled Carbon, an experimental language that is intended to match the performance of the C++ language while supporting LLVM(Low Level Vitual Machine) and being able to interoperate with existing C++ code bases. This is a big deal because C++ is by far the most widely used language for developing HIGH performance low level systems.

The issue with C++ improving now is that it needs to focus more on standardizing than on expanding the architecture to meet the needs of contemporary developers because it is such a crucial language for so many codebases.

Difficulty of transition

Technology(Tech stacks) is extremely difficult to create and alter. For instance, when Python2 was upgraded to Python3, changes weren't as drastic, but it was still incredibly difficult for anyone attempting to migrate the codebase.

Python 2 to 3
One may argue that the fact that C++ doesn't try to evolve too much is a really good thing.

The successor

Carbon is intended to replace C++, which is no easy task. However, Google is no stranger to building computer languages; it is the company behind stacks like the GO and Dart languages.

No suitable logo/symbol available yet

Currently, Carbon does not have a suitable logo or symbol, but it is possible that we might get that someday.

Wait Hold up!! Why not Rust?

Why not utilize a contemporary memory-safe language like "Rust," you may be asking. First of all, not all C++ developers like rust, but more significantly, Carbon is made to be easily adopted by modern developers and code bases that use C++, and it has bi-directional compatibility with C++, meaning you can use the current libraries in carbon easily.

Installing carbon

Let's look at how to run carbon, so at this point you need to install LLVM and Google's build tool, Bazel.

Bazel

Installing bazelisk

Bazelisk is a Go-written wrapper for Bazel that automatically chooses a suitable version of Bazel based on your current working directory, downloads it from the official server, and then seamlessly passes through any command-line inputs to the actual Bazel binary.

brew install bazelisk

bazelisk

Installing Clang/LLVM

LLVM is a library that is used to create, optimize, and generate intermediate and/or binary machine code. LLVM can be used as a compiler framework, where you provide the "front end" (parser and lexer) and the "back end" (code that converts LLVM's representation to actual machine code), and it can also act as a JIT(Just In Time) compiler.

brew install llvm

llvm

Run the following command if you must have llvm in your PATH directory first:

echo 'export PATH="/opt/homebrew/opt/llvm/bin:$PATH"' >> ~/.zshrc

The following command might need to be configured in order for compilers to find llvm.

export LDFLAGS="-L/opt/homebrew/opt/llvm/lib"export CPPFLAGS="-I/opt/homebrew/opt/llvm/include"

Fix Path

Download Carbon's code.

The last remaining step is to download and clone the whole carbon repository onto your local machine.

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

Git clone

After cloning the full repository, you may now navigate to that specific directory.

cd carbon-lang

Build and run the explorer.

As soon as you're finished, use the following command to build and launch your first "hello world" in carbon.

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

So let's navigate to the directory where that code is located.

Format only

// format_only.carbonpackage ExplorerTest api;fn Main() -> i32 {  var s: auto = "Hello world!";  Print(s);  return 0;}

Run

This hello world program has a function that utilizes Print with a string literal; it seems pretty and really straightforward, but if you've never seen a hello world in C++, it looks like this.

#include <iostream>int main() {    std::cout << "Hello World!";    return 0;}

As you can see(from above code), it feels like a fundamental shift in the code base, and it's even more noteworthy because it supports contemporary generic systems.In statically typed languages, generics allow you to give types as parameters to classes and functions, which is a fairly common pattern. C++, however, has a different technique known as templates which is frequently misused.

Although generics offer many advantages in the majority of cases, Carbon will continue to support templates, but its focus is on enhancing memory safety patterns. It basically tries to be as safe as possible without affecting performance or compatibility with C++, and depending on how much you want to prioritize, it can be run in different modes like debug, performance, and Hardened mode. Carbon has created an entire documentation for implementing safety strategy.

Safety

One trait of carbon is that it doesn't use mechanisms like borrow-checking like rust, reference counting like swift, and garbage collection like GO

Conclusion

Since you may be wondering when you may begin using this programming language, the answer is that you can do so at the moment. However, it is not yet developed enough for any production use cases, making it a highly impactful project that is currently in the early stages.


Original Link: https://dev.to/pramit_marattha/googles-carbon-language-an-alternative-to-c--3p0e

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To