Carbon Copy No.7: Classes, part 1 #5714
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
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Carbon Copy, June 2025
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 (sometimes extremely roughly) every other month.
Toolchain update
Work on the toolchain continues. We have made strides in interop! This example shows passing and returning integers to a C++ library.
Note that
inline
is accepted by the Carbon compiler, but inlining itself is not yet supported. Try this example in the Compiler Explorer.There has also been forward progress on generics, with a lot more implementation now in place. A key change is merging ("coalescing") equivalent instantiations of generics while lowering. This is Carbon's answer to automatically fixing C++ template bloat, as well as a key building block to have safety features participate in the type system. (See the PR here.)
Daily builds of the toolchain are available on our releases page, but for short experiments you can always use the Compiler Explorer which stays up-to-date.
Spotlight: Classes, part 1
Classes and instances in Carbon are designed to feel familiar to users of C++ (and, honestly, Java, C#, and Swift, too). Rather than explain too much in advance, let's present an example.
Note from Wolff: This example is about otters because it allows me to use the word "mustelid", which is an awesome word.
This should all look familiar from other languages. As elsewhere in Carbon, values are not writable, so in methods that do mutations,
self
must be aref
(#5434). Class members can only be accessed using member-access syntax, which is unlike C++ wherethis->
is implicit when referencing a member.Now, a
Mustelid
instance.Mustelid
can be extended because it is abase
class. This allows us to create anOtter
class, and we will makeOtter
abstract. (It has to be marked abstract because it contains an abstract method, but we could mark it abstract in any case.)By default, functions are not virtual, so
Slide()
cannot be overridden. However,Play
is allowed to be overridden andSwim
must be implemented in subclasses.Carbon only supports single inheritance. This is to avoid the complexity and overhead that multiple inheritance brings, particularly since it is frequently discouraged in C++. (Sometimes, inheritance needs to be more complicated than single inheritance. Carbon has interfaces and mixins to help you with your complicated data structures, and we will cover them in future issues of Carbon Copy.)
There are lots of kinds of otters out there, but we'll differentiate only between river and sea otters for simplicity. This will be our final class structure:
Play
gets tricky, because we're trying to override it, but within that method call the base method without using virtual dispatch. This example uses a proposed syntax from this GitHub issue that would directly invoke a parent implementation.Using our new class:
Let's take one more look at a subclass; this time a sea otter. For variety, let's define the methods outside the class definition.
And, try it out!
Note that this code will not compile with the toolchain as of publication. Along with calling base methods and
ref
, discussed above,protected
is not yet available, nor are initial values for member variables.Data layout
As you may remember from Carbon Copy #5, fields of a struct are laid out in the order in which they are defined, so a
Mustelid
's memory would be in the order:whisker_count
fur_status_
For inheritance chains, each subsequent derived class appends on its members. In our example, only
SeaOtter
s have another variable,extra_fur_count
, which would be appended onto theMustelid
data section.Instances of classes with virtual members have a hidden member called the
vptr
, which points to its most-derived class's virtual function table (vtable)
. This should be a familiar design from other languages, where the virtual function table contains a list of function pointers for each of the virtual methods to handle dispatch. The table has the derived class's method pointers, unless the derived class does not override it, in which case it has the parent's.Conclusion
Although most of these concepts concerning methods, members, and polymorphism are common among class systems, Carbon has taken a principled approach that reuses concepts from structs, variables, and values.
For example, Carbon classes have a focus on predictability and readability. Features that could potentially cause surprising behavior are made explicit in Carbon, such as requiring introducers for extendable classes and methods, and indicating where a function overrides or indicating whether
self
is mutable. Likewise, each member and function must be explicitly set to something besidespublic
, which makes it easy to tell at a glance whether there are access modifiers, vs. having labels that may be far from their definition.There is also a strong emphasis on simplicity. For example, multiple inheritance has always been a sharp sword, with descriptions of "the diamond of death" now almost 30 years old. In situations where you may need shared functionality across classes, Carbon provides other features like interfaces to separate interface from implementation (which we'll talk about soon!).
If you want to read the entire class design as approved now, it's here. This is an active area of toolchain implementation, so some of the features described here may change when they appear. (In fact, during the writing of this, the impl introducer became override!)
This is the first spotlight on classes. Next time, we'll delve deeper. If you have questions you'd like answered about classes, please write in and we can answer them!
Recent proposals and issues
Leads decisions:
impl fn
tooverride fn
?Merged proposals:
Other notes
We have ended the Carbon Weekly Meetup on Wednesdays (Pacific time). Thanks so much for joining us!
In its place, if you want frequent updates with lots of detail, we have a summary of activity in the Discussions tab under "Last Week In Carbon". There's even an RSS feed!
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!
Yours, someday in Iowa,
Wolff, David, and the Carbon team
Beta Was this translation helpful? Give feedback.
All reactions