-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
compiler: implement decl literals #21264
Conversation
Would you mind doing the release notes writeup in the PR description? e.g. https://ziglang.org/download/0.12.0/release-notes.html#Aggregate-Destructuring |
Done. This may need some rewording if we also rename/change |
Thanks! That helps a lot to reduce release stress :-) |
To be honest I find that langref part and this quote hard to digest, maybe additional existing example would help other people to understand that part. Rephrasing original ifreund comment:
const ArrayListUnmanaged = struct {
items: []u8 = &.{},
capacity: usize = 0,
};
// It's not obvious for reader here, that struct has another field `items` initialized with default value, which is now desynced with `capacity`.
const bogus: ArrayListUnmanaged = .{ .capacity = 42 };
// If struct would not have default fields, this code would look a lot more instantly wrong:
const bogus_visible: ArrayListUnmanaged = .{ .capacity = 42, .items = &.{} }; // something wrong... Everything else in doc is clear for me. Just some questions, please correct me if I read it wrong:
|
This is mainly useful in conjunction with Decl Literals (ziglang#9938). Resolves: ziglang#19777
In favour of newly-added decls, which can be used via decl literals.
Updated the release notes section to explain via a more concrete example -- yeah, it was a little badly worded.
That all seems essentially correct. Bear in mind, the names aren't hard rules: |
Pretty straightforward -- see commit messages. I also added "default initialization" decls to
ArrayListUnmanaged
,HashMapUnmanaged
,ArrayHashMapUnmanaged
, andGeneralPurposeAllocator
.I am not yet using these in the compiler or standard library to avoid a need for a zig1.wasm update.
Release Notes
Zig 0.14.0 extends the "enum literal" syntax (
.foo
) to provide a new feature, known as "decl literals". Now, an enum literal.foo
doesn't necessarily refer to an enum variant, but, using [Result Location Semantics](langref link), can also refer to any declaration on the target type. For instance, consider the following example:Since the initialization expression of
val
has a result type ofS
, the initialization is effectively equivalent toS.default
. This can be particularly useful when initializing struct fields to avoid having to specify the type again:It can also help in avoiding [Faulty Default Field Values](langref link), like in the following example:
Many existing uses of field default values may be more appropriately handled by a declaration named
default
orempty
or similar, to ensure data invariants are not violated by overriding single fields.Decl literals also support function calls, like this:
As before, this syntax can be particularly useful when initializing struct fields. It also supports calling functions which return error unions via
try
. The following example uses these in combination to initialize a thin wrapper around anArrayListUnmanaged
:The introduction of decl literals comes with some standard library changes. In particular, unmanaged containers, including
ArrayListUnmanaged
andHashMapUnmanaged
, should no longer be default-initialized with.{}
, because the default field values here violate the guidance discussed above. Instead, they should be initialized using theirempty
declaration, which can be conveniently accessed via decl literals:Similarly,
std.heap.GeneralPurposeAllocator
should now be initialized with its.init
declaration.The deprecated default field values for these data structures will be removed in the next release cycle.