Carbon Copy Newsletter No.3 #4068
wolffg
announced in
Announcements
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Carbon Copy, June 2024
Here is the new Carbon Copy, your periodic update on the Carbon language!
Carbon Copy is designed for those people who want a high-level view of what's happening on the project. If you'd like to subscribe, you can join [email protected]. Carbon Copy should arrive roughly every other month.
Toolchain progress
We've made significant progress on our toolchain, particularly in these areas:
import
extern
As a test, we can now compile and execute a program like this prime sieve example, if we restrict ourselves to language features that are already implemented in the toolchain:
This produces output:
...however, the toolchain is still in early development. It is not yet tested or documented, and is guaranteed not to work on many platforms. Please check back in future newsletters for more progress updates!
Carbon at Conferences
Recently
Upcoming
Recent proposals
In progress since last newsletter, now approved & merged:
New since last newsletter, now approved & merged:
New, in progress:
extend api
#3802Spotlight: Expression categories
Expressions are the portions of Carbon syntax that produce values. There are three different categories of expressions:
These three represent a small number of composable concepts that allows a simpler, faster compiler with efficient calling conventions (even in situations where types are generic). Hopefully, too, an abbreviated set of expression categories will also be easy to learn and apply.
Let's take a look at all three.
Value expressions
Value expressions produce abstract values that cannot be modified or have their address taken. They can be formed in two ways: a literal expression like
42
, or by reading the value of some stored object.Values allow efficient passing of arguments into functions. They give the compiler a lot of flexibility to use an efficient calling convention since the values may not be modified and do not have addresses. They provide a single model that can get both the efficiency of passing by copy for small types (such as those that fit into one or two machine registers) and also the efficiency of minimal copies when working with types where copies are not viable or have significant costs.
The compiler typically uses one of three representation approaches for values:
"Customized" is the hardest to imagine; think of a C++
std::string_view
that is a read-only view of the contents of a string, orstd::span
that gives a read-only view of a container.By using different value representations for different types, we get efficiency even in generic code. In C++, you might use a
const
reference for a parameter with a templated type, because it works with every type, even though that isn't the most efficient choice for integer arguments. In Carbon, passing by value uses a calling convention appropriate to the type. (Footnote: For more, you can read Foonathan's post on the prospects for efficiency.)Reference expressions
Reference expressions refer to objects with storage where a value may be read or written and the object's address can be taken. These are analogous to C++'s lvalues or Rust's place expressions.
Most reference expressions are durable, which means the object's storage outlives the full expression and the address could be meaningfully propagated out of it as well.
Initializing expressions
Initializing expressions do what they sound like; they initialize an object. They require storage to be provided implicitly when evaluating the expression.
Function calls in Carbon are modeled directly as initializing expressions; they require storage as an input and when evaluated cause that storage to be initialized with an object. Consider when a function call is used to initialize some variable pattern:
The return value is constructed directly into the caller's storage. This enables Carbon to guarantee that returned values are not copied (except for when the type is something like an integer that enables returning in a register) either:
return
statement ("return value optimization" or RVO), orreturned var
, andreturn var
("named RVO" or NRVO, docs: overview, details), as in this example:This means we didn't have to have separate constructor functions for Carbon types with their own syntax and rules. Instead, ordinary functions may be used, even for types that can't be copied.
This also means you guarantee when the compiler uses NRVO, rather than depending on "named return value optimization" of C++.
If you want a function parameter to have its own storage so the function has its own copy of the value it can modify, you can add a
var
parameter. Avar
parameter is initialized with an initializing expression, rather than a value expression. Inside the function, the parameter is a reference expression.Conversions between categories
Expressions in one category can be converted to any other category when needed.
This implicit conversion uses the
ImplicitAs
operator (see proposal & PR), which is a regular function that takes a value and returns an initializing expression.This involves a copy from the storage of the source to the destination.
Temporary materializations produce ephemeral (rather than durable) reference expressions. They still refer to an object with storage, but it may be storage that will not outlive the full expression. Beyond this distinction, there is no restriction on how they can be used.
These conversion steps combine to provide the transitive conversion table:
Conclusion
We've introduced many core concepts here. However, to write code in Carbon, you really only need to remember two basic rules:
var
, it is a reference expression, and you have permission to mutate the object. Everyvar
has its own storage, so mutations don't affect other variables.var
, it is a value expression and is immutable, and that allows optimizations.Functions return initializing expressions that are converted to one of those two cases.
Learn more
There's more to read! See:
We also strongly recommend Jonathan Müller's blog post entitled "Carbon’s most exciting feature is its calling convention" as a deep dive into what efficiencies can be had with Carbon's function parameters.
Other notes
If you want more current discussion, check out the weekly meeting notes from the Carbon Weekly Sync.
Wrap-up
Don't forget to subscribe! You can join [email protected]. If you have comments or would like to contribute to future editions of Carbon Copy, please reach out. And, always, join us any way you can!
Carbonically yours,
Josh, Wolff, and the Carbon team
Beta Was this translation helpful? Give feedback.
All reactions