Carbon Programming Language

Carbon Programming Language
Carbon Programming Language

Carbon, also known as Carbon-Lang, is an experimental general-purpose programming language that was designed to be a “C++ successor.” Google initiated the open-source initiative, which follows in the footsteps of earlier Google-created programming languages (Go and Dart). Carbon was initially announced by Google developer Chandler Carruth in July 2022 at the CppNorth conference in Toronto.

Designed byGoogle
FamilyC
LicenseApache-2.0
Typing disciplineStatic, nominative, partially inferred
Implementation languageC++
Filename extensions.carbon

The language aims to address numerous identified flaws in C++ while offering a similar feature set. The language’s key aims are readability and “bi-directional compatibility,” as opposed to adopting a new language like Rust (which, while based on C++, is not compatible with C++ applications). The Carbon leaders will make decisions on linguistic changes.

Carbon’s concept, implementation, and accompanying tools are available on GitHub under the Apache-2.0 license with LLVM Exception.

The following code demonstrates how to write a “Hello, World!” application in Carbon:

package Sample api;    

fn Main() -> i32 {
    Print("Hello, World!");
    return 0;
}

Why build Carbon?

With huge and rising codebases and investments, C++ remains the leading programming language for performance-critical applications. However, as previously said, it is failing to evolve and satisfy the demands of developers, owing in part to decades of technological debt. Incrementally upgrading C++ is exceedingly difficult, due to both technical debt and difficulties with the evolution process. To overcome these issues, it is advisable to avoid directly inheriting the history of C or C++ and instead begin with robust language foundations such as a current generics system, modular code organisation, and consistent, straightforward syntax.

Existing contemporary languages, such as Go, Swift, Kotlin, and Rust, already provide a fantastic developer experience. Developers who are fluent in one of these current languages should do so. Unfortunately, the design of these languages create major impediments to adoption and migration away from C++. These impediments vary from changes in software idiomatic design to performance overhead.

Carbon is primarily a successor language approach rather than an incremental evolution of C++. It is centred on C++ compatibility as well as broad acceptance and migration for current C++ codebases and developers. A successor language for C++ requires:

  • C++ performance matching is an important attribute for our developers.
  • Seamless, bidirectional compatibility with C++, allowing any library in an existing C++ stack to use Carbon without having to transfer the rest.
  • Migration that is scalable, including some source-to-source translation for idiomatic C++ programming.
  • For C++ developers, a gentle learning curve with reasonable familiarity.
  • Comparable expressivity and support for the design and architecture of current software.

We can build on top of C++’s current ecosystem, bringing existing investments, codebases, and developer populations with us. This strategy has been adopted by a few languages for various ecosystems, and Carbon seeks to fulfil a comparable role for C++:

  • JavaScript → TypeScript
  • Java → Kotlin
  • C++ → Carbon

Language Objectives

Carbon is being designed to support:

  • High-performance software
  • Evolution of software and languages
  • Code that is simple to read, comprehend, and write
  • Mechanisms for practical safety and testing
  • Development that is quick and scalable
  • Contemporary operating systems, hardware architectures, and environments
  • Interoperability with current C++ code and migration from it

While many languages share subsets of these aims, Carbon differentiates itself by combining them.

We also have clear non-goals for Carbon, most notably:

  • For the entire language and library, a stable application binary interface (ABI).
  • Complete backwards and forwards compatibility

Our full objectives paper fleshes on these ideas and gives a more in-depth look at our Carbon project and language aims.

The Language – Carbon

“Carbon is an experiment to examine a hypothetical, distant future for the C++ programming language based around a certain set of goals, priorities, and use cases,” as stated in the Goals.

Syntax

Among the aspects discussed are the following:

  • Introducer keywords include fn for function declarations and var for variable declarations.
  • The function input parameters are read-only.
  • Pointers allow for indirect access and modification.
  • Name type expressions
  • The root namespace is always local.
  • By default, public members. The logic appears to be that since you would be reading the public functions in your API the majority of the time, it makes sense to expose them.
  • Generics verification

This is by no means an exhaustive list, but it provides an overview of some of the language’s qualities.

// 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 Language: Language Fundamentals

If you’re familiar with C, C++, or other programming languages, you’ll be familiar with Carbon syntax.

Carbon is still an experimental material, thus certain aspects may change. “Note that Carbon is not yet ready for usage,” says the documentation. Some design concepts, on the other hand, are less likely to alter.

Let’s start with the code in format only.carbon.

package ExplorerTest api;
fn Main() -> i32 {
  var s: auto = "Hello world!";
  Print(s);
  return 0;
}
  • Packages are declared with the package keyword. This file adds to the default library.
  • The fn introducer keyword is used to declare functions.The code fn Main() -> i32… declares the Main function. The return type of Main() is i32, which stands for integer. Main always returns zero.
  • The variable declaration is introduced with the var keyword. The variable s is named in the code above, followed by a colon: and the type. The variable type is inferred automatically using auto.
  • The keyword let can be used to declare constants. While this may be perplexing for developers coming from JavaScript, it will be simple for Swift developers.
  • Finally, two slashes / can be used to add comments.

This little file provides an excellent understanding to Carbon syntax.

Date of publication of Carbon language

While carbon language is still at the experimental stage, it is unknown when it will be ready for general use. A large number of simple use examples are currently not supported. I’m hoping to have this up and running shortly.

Carbon Language Numeric Variables:

Carbon language variables can be

  • bool stands for boolean true or false.
  • Integer types include i8, i16, i32, i64, i128, and i256.
  • Unsigned integer types include u8, u16, u32, u128, and u256.
  • float types include f16, f32, f64, and f128.
  • For digit separators, can be used. For example, if 1 000 000 is expressed without quotations, it remains an integer.

Strings in the Carbon programming language:

Strings can be defined using the syntax

  • String representing a byte sequence
  • StringView as a read-only reference for a byte sequence in utf-8.

There are two ways to declare string literals.

  • Use a double quote mark () for a single line “) denotes a single line.
  • Use multi line string declaration for multi line string declaration ” (“””)

Carbon language memory safety:

Carbon language places a strong emphasis on memory management, particularly memory safety. Carbon language intends to ensure memory safety at a high level by employing

  • Spatial Memory Security
  • Temporal memory security Here is a full instruction on how carbon achieves memory safety, as well as aims and fundamentals. For secure memory management, look at Carbon language memory safety.

Total
0
Shares

Leave a Reply

Related Posts