Carbon Copy Newsletter No.5 #4694
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, December 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 announce@carbon-lang.dev. Carbon Copy should arrive roughly (sometimes very roughly) every other month.
Toolchain progress
We've made a lot of progress on generics in the toolchain, which now supports generic classes and interfaces. This is being used to implement integers, such as
i32
, as a generic library type parameterized by the bitwidth,Core.Int(N)
, following our "All APIs are library APIs" principle.Earlier this year, we noted some ambitions (full talk) for our compile-time performance, and we've been making steady progress. This includes developing a robust compile-time benchmarking framework, and writing an optimized hash table and hashing function. The hash tables are used for keeping track of the identifiers encountered while lexing the input source code. Using our benchmarking infrastructure, we get these results on an Apple M1:
345.01M/s
9.79M/s
58.01M/s
200.71M/s
5.69M/s
33.75M/s
47.94M/s
1.36M/s
8.06M/s
We also have an exciting Carbon prototype solution to the Advent of Code day one challenge! It is far from what we expect long term, requiring lots of workarounds and stubbing out capabilities. Right now Advent of Code challenges are still more useful for driving toolchain improvements than for folks to learn about Carbon, but stay tuned for next year!
Spotlight: Structs
This issue we're going to talk about structs in Carbon. Structs are lightweight types that are used for when you need more than a tuple but less than a class. It allows you to name the members of your tuples and refer to them in arbitrary order.
(If you haven't read about patterns and tuples yet, see the Spotlight in Carbon Copy #4! As we look at structs, the syntax should look pretty familiar.)
A struct is just a list of data member names and types. Struct and struct values have a literal syntax written using curly braces
{
...}
, allowing you to write them in code. They both name their fields using designators, written with a period (.
) followed by the name of the field. The difference between them is that struct values follow the designator with "= value" while struct types use ":
type".We've used the term fields here to refer to a data member that has typed storage within each instance. (Classes, too, can have data members, but they also have functions, methods, associated constants, and so on.)
An intended use case of struct values is to write out examples in source code that may be in a test.
In C++, if you want to have an object that contains multiple values, you have to pre-declare the type and give it a name, but with structs you can just write something out in the code.
Struct values are also convenient for return values.
When not marked as references, struct values are passed by "most-convenient" in function calls and returns, which means they may be references, copies, registers, or something else as best determined by the compiler. (From the code's perspective, you can't tell the difference!)
It's worth reminding ourselves here that structs are types, which means they are checked at compile time. This means that you can't return a struct with an arbitrary number of fields or null; you must return exactly the type specified.
Comparisons and initialization
When initializing a struct value, you use
=
. Initialization is in the left-side order, even if you write them in some other order on the right.When comparing structs, equality is field-wise. The comparisons happen in the order of left side struct's fields, with short-circuiting. So, this:
is equivalent to:
Inequalities are only permitted if fields are in the same order.
A useful property is that
a = b
results ina == b
, even whena
andb
have different field orders.Memory
The fields of a struct are laid out in memory in the same order as they are written in the source. (Due to padding, this may mean that the struct takes up more memory than a different order.) The Carbon language favors such predictability, which gives the developer control and allows the compiler to be simpler and more transparent.
Like
var
/let
from Carbon Copy #3, you can usevar
to create objects which have addresses and are mutable. Withoutvar
, they are values which are only readable, and have no fixed location.1Remember that values are much more flexible for the compiler; it can choose where to store it, and figure out what lifespan it needs. (Read more on Carbon Copy #2!)
You can mix and match variables, values, and pointers, as struct definitions are also patterns like function definitions.
Initializing classes with structs
It's reasonable and expected to use a struct to initialize an instance of a class. This takes advantage of the fact that the class and the struct literal have the same field names.
Conclusion
As we said at the start, structs and struct values are there when you need more than a tuple but less than a class. They have predictable structure and simple memory layout, and conform syntactically to the patterns we have seen elsewhere in Carbon. They are workhorses for in-code literals, and they are fantastic for passing data around, both in argument lists and return values.
(Did you notice that we never needed a
struct
keyword?)One question you might ask after reading all this: Is a struct a class? Sort of. A struct is an anonymous data class, which means it is a container that has named data in it. A data class is the basis for a class (which contains named data, too), but an actual class would be named, can have methods, and wouldn't be usable in the same way.
Speaking of classes, in the next Spotlight we'll talk all about them!
Recent proposals and issues
Approved & merged proposals since last newsletter:
New leads questions:
impl
? #4579private
andprotected
members? #4575Carbon at Conferences
Recent
See all talks here.
Other notes
If you want more about the current discussion, check out the weekly meeting notes from the Carbon Weekly Sync.
Wrap-up
Don't forget to subscribe! You can join announce@carbon-lang.dev. 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!
Allotropically yours,
Josh, Wolff, and the Carbon team
Notes
Footnotes
Writing that out makes values sound sad and lonely. They aren't! They are loved (well, they are "valued") for as long as they are needed. ↩
Beta Was this translation helpful? Give feedback.
All reactions