Carbon Programming Language - A Successor to C++

Posted in

Carbon Programming Language - A Successor to C++
anamika

Anamika Kalwan
Last updated on April 27, 2024

    A programming language is a computer language that developers or programmers leverage to create computer programs or software applications that serve certain purposes. There is a plethora of programming languages available out there, and new programming languages continuously emerge. Among the new programming languages, Carbon is a new one.

    Java has Kotlin as its successor, C has C++, Objective-C has Swift, and JavaScript has TypeScript. But what about C++? There was no successor to C++ till the introduction of the Carbon language. With this language, the simple and beginner's language, C++, has got its successor.

    Microsoft has designed TypeScript to upgrade JavaScript and Kotlin to overcome the weaknesses of Java. Similarly, Google introduced Carbon to provide programmers with the experience that they are expecting with C++.

    Chandler Carruth, a technical lead for Google’s core programming languages, first introduced Carbon in July 2022 at the CppNorth conference in Toronto. At this conference, he said that he had designed this new language as the successor of the C++ programming language and would release it in 2024 or 2025.

    This blog post aims to make you familiar with this new programming language, Carbon.

    So, let us begin our discussion!

    What is the Need for the Carbon Programming Language?

    Carbon programming language is a new, open-source, experimental successor to the C++ programming language . C++ is the most preferred and dominant language for developing applications where performance plays a vital role due to huge codebases and investments. However, it is challenging to improve C++ incrementally to meet the expectations of developers and programmers.

    So, Google’s technical lead, Chandler Carruth, decided to design a new programming language from scratch instead of addressing the C++ issues by inheriting the language. He designed Carbon by keeping in mind solid language foundations, like modular code generation, modern generics system, and simple language syntax.

    Modern programming languages, like Go, Swift, Kotlin, Rust, and many others, stand true when it comes to providing the experience the developers or programmers expect. So, if it is possible for developers to use any of these existing modern programming languages, they should.

    Unfortunately, developers experience barriers that range from changes in the idiomatic design of software to performance overhead. These barriers make it difficult for them to migrate from C++ to other modern programming languages.

    Furthermore, Carbon is not an incremental evolution of C++; instead, it is fundamentally a successor language approach. Carruth has designed this language by focusing on interoperability with C++. The following are a few points that the successor of C++ should include:

    • The performance that matches C++.
    • A shallow learning curve, making it easier and quicker to learn for developers having familiarity with C++.
    • Bidirectional interoperability with C++. This means that a library present on an existing C++ stack can adopt Carbon without porting the rest.
    • Support the existing design and architecture of software programs.
    • Comparable expressivity.
    • Scalable migration.

    Why is it Difficult to Evolve C++?

    Language and governance are the two points that make it difficult to evolve C++. Let us discuss these two points below.

    Language

    • Technical Debt: The decisions made before to make C++ a great and wonderful language are now posing a burden. There has been an accumulation of a lot of technical debts for C++.
    • Prioritization of Backward Compatibility: Since the inception, there has been the addition of a lot of features rather than replacement or removal. So, when it comes to backward compatibility, it costs a lot.

    Governance

    • Process: The improvements to C++ follow the bureaucratic committee approach whose priority is standardization rather than the design.
    • Limited Access: Access to the committee and standard is restricted and expensive.
    • Interests of Few: Though many companies and countries show interest in improving C++, many stakeholders still need to.
    • Lengthy Decision Process: The decision process is lengthy, and sometimes it cannot reach any conclusion.

    Though Rust can be used as a successor to C++, moving Rust to the C++ ecosystem is pretty challenging. Meanwhile, Carbon focuses on large codebases that are difficult to convert into Rust.

    Goals of the Carbon Programming Language

    The following are the goals of the Carbon programming language:

    Goals of Carbon Programming Language

    Many modern languages feature the subset of the aforementioned set of goals. What makes Carbon different from other languages is it features the above entire set of goals.

    Carbon: The Language

    It is an experimental successor to C++. In other words, we can say that it is an experiment to find out the possible and long-term future of the C++ language designed around a certain set of goals, use cases, and priorities. As mentioned above, it is an experimental project and does not have any working compiler or toolchain.

    The following are some worth mentioning features of Carbon:

    features of Carbon programming language

    Though the above list does not include the entire feature, it provides an overview of the characteristics of Carbon.

    Syntax

    The syntax of Carbon is analogous to Rust and other programming languages. Let us have a look at the syntax of Carbon’s functions, variables, loops, conditionals, and classes.

    Variables

    As the language is statically typed, you need to define the data type of the variable while declaring. You can declare a variable using the var keyword.

    For example,

    var hello: auto = "Welcome to the World of Techies!";

    Here, auto is a data type that is general that you can use with any variable. Also, the statement ends with a semicolon, analogous to C++.

    Functions

    The function declaration in Carbon is similar to Rust. The only difference is that you need to declare function parameters explicitly.

    For example,

    fn Sum(var a: i32, var b: i32) -> i32 {
    return a + b;
    }

    The Sum function calculates the sum of function arguments.

    Loops

    Carbon uses a C-style for loop that includes the range functionality.

    For example,

    for (var emp_name: String in employees) { // employees is an array
    Print(emp_name);
    }

    The above for loop iterates over the array named ‘employees’ and prints the result.

    Classes

    To define classes in Carbon, you need to use the keyword ‘class’.

    For example,

    classEmployees {
    var name: String;
    var ID: i32;
    var Contact: i32;
    }

    Here, we have created the class ‘Employees’ with three different fields: name, ID, and Contact.

    Conditionals

    Like loops, Carbon follows C-style conditional loops.

    For example,

    fn verify(var value: i32) -> i32 {
    if (value == 9) {
    Print(value);
    }
    else {
    Print(0);
    }
    }

    The above function ‘verify’ checks the value of the variable. If it is equal to 9, it prints that value; otherwise, it prints 0.

    What Does Carbon Promise?

    As we have discussed the challenges in the language and governance of C++, Carbon follows a different approach for both of these aspects. Let us discuss that approach below.

    Language

    As Carbon is not an incremental evolution of C++, it is a language that is designed from scratch that includes:

    • Modern generics system
    • Modular code organization
    • Simple and consistent syntax

    Governance

    Carbon aims to be more diverse by:

    • Developing on open-source principles, tools, and processes.
    • Possessing the governance structure that makes it easy to make decisions rapidly.
    • Enlarging the ecosystem by incorporating tools, such as IDEs , compilers , and a standard library, that provide developers with a rich experience.
    • Introducing a built-in package manager to overcome the issue in the C++ ecosystem.

    Generics

    Regarding generics, Carbon supports the modern generic system with checked definitions. It also supports opt-in templates for seamless interoperability with C++. In comparison to C++ templates, the modern generics system with checked definitions provides a lot of benefits, as follows:

    • Generic definitions are completely type-checked. This eliminates the need to instantiate to check for errors. Also, it saves the compile-time cost of checking the definitions after every instantiation. You will get exact usage error messages that state which requirements are not met.
    • There is no need for a separate implementation for dynamic dispatch as it takes place automatically. This, in turn, reduces the binary size and allows constructs like heterogeneous containers.
    • Checked interfaces imply fewer accidental dependencies on implementation details.

    Memory Safety

    Memory safety is something where C++ faces a great challenge, and this is what the successor language needs to address. Carbon focuses on:

    • Tracking uninitialized states, enforcing initialization, and providing hardening against initialization bugs.
    • Designing fundamental APIs and debugging to support dynamic bound checks in debugs and hardened builds.
    • Having a cheaper and more comprehensive default debug build mode than existing C++ build modes.

    Carbon vs C++: Code

    If you are already familiar with C++, Carbon should be easy for you to learn and write. Both Carbon and C++ are statically and strongly typed and object-oriented . While C++ is less expressive, Carbon is more expressive.

    Let us look at C++ and Carbon code to calculate the area of a circle. Both the code snippets are taken from GitHub .

    C++

    #include <math.h>
    #include <iostream>
    #include <span>
    #include <vector>
    
    structCircle {
    float r;
    };
    voidPrintTotalArea(std::span<Circle> circles) {
    float area = 0;
    for (const Circle& c : circles) {
    area += M_PI * c.r * c.r;
    }
    std::cout << "Total area: " << area << "\n";
    }
    
    automain(int argc, char** argv) -> int {
    std::vector<Circle> circles = {{1.0}, {2.0}};
    // Implicitly constructors `span` from `vector`.
    PrintTotalArea(circles);
    return0;
    }

    Carbon

    package Geometry api;
    import Math;
    classCircle {
    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);
    }
    
    fn Main() -> i32 {
    // A dynamically sized array, like `std::vector`.
    var circles: Array(Circle) = ({.r = 1.0}, {.r = 2.0});
    // Implicitly constructs `Slice` from `Array`.
    PrintTotalArea(circles);
    return0;
    }

    As there is no compiler available for Carbon as of now, you can interpret the Carbon code using the Carbon Explorer. You can interpret the above Carbon code at compiler-explorer.com and print its result.

    Advantages of Carbon Programming Language

    The following are some remarkable benefits of the Carbon programming language:

    Advantages of carbon programming language

    • Fast Development: Carbon’s simple and intuitive syntax facilitates code development and deployment.
    • Scalability: The language is strongly scalable and is efficient in handling large volumes of traffic and data. Hence, it is widely used to develop applications that need to handle a massive amount of data and users.
    • Cross-Platform Compatibility: Carbon can work on all major operating systems, including Windows, Linux, and macOS.
    • Easy to Learn: The uncomplicated syntax of the language makes it easy for beginners to learn and code.
    • Community Support: The community is large and has experienced members who help others and strive to improve the language.
    • Extensive Library of Resources: With extensive resources, including documentation, code examples, and tutorials, you can learn the language easily.

    Conclusion

    This was all about the overview of the Carbon programming language. It is an experimental successor of C++ that aims to provide performance that matches C++, seamless interoperability with C++, scalable migration, and support for existing software architecture. As a successor of C++, it will have a gentle learning curve if you already have an acquaintance with C++.

    We hope this article has helped you gain a better understanding of Carbon.

    People are also reading:

    FAQs


    The Carbon language is used as the successor of the C++ language. It is an experimental, open-source, and general-purpose programming language.

    C++ is a beginner-friendly language. As Carbon is a modern alternative to C++ that provides the matching performance and the ability to connect to existing C++ code, it may replace C++.

    No, you cannot use the Carbon language now, as it is still in its development phase and has not been released for use.

    Carbon will be a language that will primarily be focused on code that is easy to read and understand. Also, it will be a fast and scalable language in development. So, it will be worth learning Carbon.

    Leave a Comment on this Post

    0 Comments