-
-
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
special forms of struct and union which allow setting properties via comptime values #8643
Comments
|
Builtins are already a special case in the parser. The spec grammar will need to have some modifications made, but that would have been the case for any solution to this problem. |
From the syntax PoV I prefer what's proposed in #4245 where the optional UnionProperties/StructProperties go between
The only possible downside I see is that a user may specify a conflicting set of parameters (eg. Revised example usage const S = struct (.{ .layout = .Extern }) {
field1: i32,
field2: f64,
pub fn foo() void {}
}; |
Especially once we have #1717 I'm not sure why we should prefer this over enhancing const S = @Type(.{
.Struct = .{
.layout = .Extern,
.fields = &[_]StructField{
.{ .name = "field1", .field_type = i32 },
.{ .name = "field2", .field_type = f64 },
},
.decls = &[_]Declaration{
.{ .name = "foo", .is_pub = true, .data = .{ .Fn = fn () void{} } },
},
.is_tuple = false,
},
}); With some default values in std.builtin types and/or some helper functions, this could become more ergonomic. |
This might run into #4630 or become a workaround for it. |
Add this to std.builtin:
New builtins and syntax
Use these in place of
struct
andunion
keywords respectively.Existing
struct
andunion
syntax is unchanged. This is not a breaking proposal.Example usage
Motivation and real use case
The use case is having both safety in a safe build, and well-defined memory layout in release mode, for fast serialization/deserialization. We have exactly this use case in self-hosted compiler, for the
Zir.Inst.Data
union. I would imagine pretty much every video game will have this same use case.It is also planned to experiment with adding safety to structs as well, so the same use case applies there as well.
Considerations with other open proposals
extern
modifier for structs to mean "guaranteed layout", not "for use with C". #6700 now becomes a proposal to breakContainerLayout
into more pieces: ordering, default alignment, allow non-C-ABI types. You can see howStructProperties
andUnionProperties
can be updated to support this, and the syntax proposed in this issue remains the same.extern
withextern(...)
andlayout(...)
#4245 gets closed in favor of this oneThe text was updated successfully, but these errors were encountered: