Skip to content

Latest commit

 

History

History
436 lines (330 loc) · 20 KB

faq.md

File metadata and controls

436 lines (330 loc) · 20 KB

Project FAQ

Table of contents

What is Carbon?

The Carbon Language is an experimental successor to C++. It is an effort to explore a possible future direction for the C++ language given the difficulties improving C++.

What is Carbon's status?

Carbon is still an experiment. There remain significant open questions that we need to answer before the project can consider becoming a production effort. For now, we're focused on exploring this direction and gaining information to begin answering these questions.

How soon can we use Carbon?

Carbon is still years away — even if the experiment succeeds, it's unlikely that it will be ready for serious or production use in the next few years. Everything here is part of a long-term investigation.

Why make Carbon public while it's still an experiment?

One of the critical questions we need to answer as part of this experiment is whether the direction we're exploring with Carbon has both broad and significant interest for the industry at large. We feel like this is best answered by developing the language openly, publicly, and with broad participation.

How complete is Carbon's design?

We've resolved several of the most challenging language design technical decisions we anticipated based on experience with C++ and its constraints, particularly around generics and inheritance. Beyond those two areas, we have initial designs for class types, inheritance, operator overloading, syntactic and lexical structure, and modular code organization. We are aiming to complete the initial 0.1 language design around the end of 2022 although there are a large number of variables in that timeline. See our roadmap for details.

References:

How many people are involved in Carbon?

Prior to going public, Carbon has had a couple dozen people involved. GitHub Insights provides activity metrics.

Is there a demo?

Yes! A prototype interpreter demo explorer can be used to execute simple examples. For example:

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

Example source files can be found under /explorer/testdata.

Carbon can also be explored interactively on https://carbon.compiler-explorer.com.

Why build Carbon?

See the project README for an overview of the motivation for Carbon. This section dives into specific questions in that space.

Why is performance critical?

Performance is critical for many users today. A few reasons are:

  • Cost savings: Organizations with large-scale compute needs care about software performance because it reduces hardware needs.
  • Reliable latency: Environments with specific latency needs or concerns with bounding tail latency need to be able to control and improve their latency.
  • Resource constraints: Many systems have constrained CPU or memory resources that require precise control over resource usage and performance.

What level of C++ interoperability is expected?

Carbon code will be able to call C++, and the other way around, without overhead. You will be able to migrate a single library to Carbon within a C++ application, or write new Carbon on top of their existing C++ investment.

While Carbon's interoperability may not cover every last case, most C++ style guides (such as the C++ Core Guidelines or Google C++ Style Guide) steer developers away from complex C++ code that's more likely to cause issues, and we expect the vast majority of code to interoperate well.

For example, considering a pure C++ application:

A snippet of C++ code. Follow the link to read it.

It's possible to migrate a single function to Carbon:

A snippet of mixed Carbon and C++ code. Follow the link to read it.

References:

What would migrating C++ code to Carbon look like?

Migration support is a key long-term goal for Carbon.

If a migration occurs, we anticipate:

  • Migration tools that automatically translate C++ libraries to Carbon at the file or library level with minimal human assistance.
  • Bidirectional C++ interoperability that allows teams to migrate libraries in any order they choose without performance concerns or maintaining interoperability wrappers.
  • Test-driven verification that migrations are correct.

What alternatives did you consider? Why did they not work?

Why not improve C++?

A lot of effort has been invested into improving C++, but C++ is difficult to improve.

For example, although P2137 was not accepted, it formed the basis for Carbon's goals.

Why not fork C++?

While we would like to see C++ improve, we don't think that forking C++ is the right path to achieving that goal. A fork could create confusion about what code works with standard C++. We believe a successor programming language is a better approach because it gives more freedom for Carbon's design while retaining the existing C++ ecosystem investments.

Why not Rust?

If you can use Rust, ignore Carbon

If you want to use Rust, and it is technically and economically viable for your project, you should use Rust. In fact, if you can use Rust or any other established programming language, you should. Carbon is for organizations and projects that heavily depend on C++; for example, projects that have a lot of C++ code or use many third-party C++ libraries.

We believe that Rust is an excellent choice for writing software within the pure Rust ecosystem. Software written in Rust has properties that neither C++ nor Carbon have. When you need to call other languages from Rust, RPCs are a good option. Rust is also good for using APIs implemented in a different language in-process, when the cost of maintaining the FFI boundary is reasonable.

When the foreign language API is large, constantly changes, uses advanced C++ features, or makes architectural choices that are incompatible with safe Rust, maintaining a C++/Rust FFI may not be economically viable today (but it is an area of active research: cxx, autocxx, Crubit).

The Carbon community is looking for a language that existing, large, monolithic C++ codebases can incrementally adopt and have a prospect of migrating away from C++ completely. We would be very happy if Rust could be this language. However, we are not certain that:

  • Idiomatic, safe Rust can seamlessly integrate into an existing C++ codebase, similarly to how TypeScript code can be added to a large existing JavaScript codebase.
  • Developers can incrementally migrate existing C++ code to Rust, just like they can migrate JavaScript to TypeScript one file at a time, while keeping the project working.

See Carbon's goals for an in-depth discussion of Carbon's vision for C++/Carbon interop and migration.

Why is adopting Rust difficult for C++ codebases?

Large existing C++ codebases almost certainly made architectural choices that are incompatible with safe Rust. Specifically:

  • Seamless interop where existing, unmodified C++ APIs are made callable from safe Rust requires the C++ code to follow borrow checking rules at the API boundary.
    • To reduce the amount of Rust-side compile-time checking that makes interop difficult, C++ APIs can be exposed to Rust with pointers instead of references. However, that forces users to write unsafe Rust, which can be even more tricky to write than C++ because it has new kinds of UB compared to C++; for example, stacked borrows violations.
  • Seamless interop where safe Rust APIs are made callable from C++ requires C++ users to follow Rust borrow checking rules.
  • Incremental migration of C++ to safe Rust means that C++ code gets converted to Rust without major changes to the architecture, data structures, or APIs. However Rust imposes stricter rules than C++, disallowing some design choices that were valid in C++. Therefore, the original C++ code must follow Rust rules before attempting a conversion.
    • Original C++ code must be structured in such a way that the resulting Rust code passes borrow checking. C++ APIs and data structures are not designed with this in mind.
    • Migrating C++ to unsafe Rust would still require the code to follow Rust's reference exclusivity and stacked borrows rules.

Why not a garbage collected language, like Java, Kotlin, or Go?

If you can use one of these languages, you absolutely should.

Garbage collection provides dramatically simpler memory management for developers, but at the expense of performance. The performance cost can range from direct runtime overhead to significant complexity and loss of control over performance. This trade-off makes sense for many applications, and we actively encourage using these languages in those cases. However, we need a solution for C++ use-cases that require its full performance, low-level control, and access to hardware.

How will Carbon work?

What compiler infrastructure is Carbon using?

Carbon is being built using LLVM, and is expected to have Clang dependencies for interoperability.

How will Carbon's bidirectional C++ interoperability work?

The Carbon toolchain will compile both Carbon and C++ code together, in order to make the interoperability seamless.

For example, for import Cpp library "<vector>", Carbon will:

  • Call into Clang to load the AST of the vector header file.
  • Analyze the AST for public APIs, which will be turned into names that can be accessed from Carbon; for example, std::vector is Cpp.std.vector in Carbon.
  • Use Clang to instantiate the Cpp.std.vector template when parameterized references occur in Carbon code.
    • In other words, C++ templates will be instantiated using standard C++ mechanisms, and the instantiated versions are called by Carbon code.

Some code, such as #define preprocessor macros, will not work as well. C++ allows arbitrary content in a #define, and that can be difficult to translate. As a consequence, this is likely to be a limitation of interoperability and left to migration.

How do Carbon generics differ from templates?

Carbon's generic programming support will handle both templates (matching C++) and checked generics (common in other languages: Rust, Swift, Go, Kotlin, Java, and so on).

The key difference between the two is that template arguments can only finish type-checking during instantiation, whereas generics specify an interface with which arguments can finish type-checking without instantiation. This has a couple important benefits:

  • Type-checking errors for generics happen earlier, making it easier for the compiler to produce helpful diagnostics.
  • Generic functions can generate less compiled output, allowing compilation with many uses to be faster.
    • For comparison, template instantiations are a major factor for C++ compilation latency.

Although Carbon will prefer generics over templates, templates are provided for migration of C++ code.

References:

What is Carbon's memory model?

Carbon will match C++'s memory model closely in order to maintain zero-overhead interoperability. There may be some changes made as part of supporting memory safety, but performance and interoperability will constrain flexibility in this space.

How will Carbon achieve memory safety?

See memory safety in the project README.

References:

How will the Carbon project work?

Where does development occur?

Carbon is using GitHub for its repository and code reviews. Most non-review discussion occurs on our Discord server.

If you're interested in contributing, you can find more information in our Contributing file.

How does Carbon make decisions?

Any interested developer may propose and discuss changes to Carbon. The Carbon leads are responsible for reviewing proposals and surrounding discussion, then making decisions based on the discussion. As Carbon grows, we expect to add feature teams to distribute responsibility.

The intent of this setup is that Carbon remains a community-driven project, avoiding situations where any single organization controls Carbon's direction.

References:

What happens when a decision was wrong?

Carbon's evolution process is iterative: when we make poor decisions, we'll work to fix them. If we realize a mistake quickly, it may make sense to just roll back the decision. Otherwise, a fix will need to follow the normal evolution process, with a proposal explaining why the decision was wrong and proposing a better path forward.

What license does Carbon use?

Carbon is under the Apache License v2.0 with LLVM Exceptions. We want Carbon to be available under a permissive open source license. As a programming language with compiler and runtime library considerations, our project has the same core needs as the LLVM project for its license and we build on their work to address these by combining the Apache License with the LLVM Exceptions.

Why make Carbon open source?

We believe it is important for a programming language like Carbon, if it is successful, to be developed by and for a broad community. We feel that the open source model is the most effective and successful approach for doing this. We're closely modeled on LLVM and other similar open source projects, and want to follow their good examples. We've structured the project to be attractive for industry players big and small to participate in, but also to be resilient and independent long-term.

The open source model, particularly as followed by Apache and LLVM, also provides a strong foundation for handling hard problems like intellectual property and licensing with a broad and diverse group of contributors.

Why does Carbon have a CLA?

Carbon uses a CLA (Contributor License Agreement) in case we need to fix issues with the license structure in the future, something which has proven to be important in other projects.

Any changes to the license of Carbon would be made very carefully and subject to the exact same decision making process as any other change to the overall project direction.

Initially, Carbon is bootstrapping using Google's CLA. We are planning to create an open source foundation and transfer all Carbon-related rights to it; our goal is for the foundation setup to be similar to other open source projects, such as LLVM or Kubernetes.

Who pays for Carbon's infrastructure?

Carbon is currently bootstrapping infrastructure with the help of Google. As soon as a foundation is ready to oversee infrastructure, such as continuous integration and the CLA, we plan to transfer them so they are run by the community.