-
-
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
Proposal: packed struct(Backing)
-> packed(Backing) struct
, support packed(Backing) union
#19395
Comments
Small bikeshed: I think it would make sense to make |
@InKryption Sure, that's a larger change than necessary for the proposal, but it does better reflect the logic of status-quo. |
I dont read |
if this were to change I'd prefer |
That won't work. It makes the syntax require lookaheads and looks terribly inconsistent overall. |
|
|
Background
In status-quo,
packed struct
andpacked union
types are guaranteed to occupy the minimum number of bits required.For
packed struct
this is the sum of the number of bits of their fields,for
packed union
(which status-quo requires to be untagged), it's the maximum number of bits of its states.Status-quo allows the syntax
packed struct(Backing)
to specify the backing integer type,which is a convenient way to have the compiler assert the number of bits occupied.
(side note: currently undocumented in the corresponding langref section.)
I think the same mechanism would be useful to have for
packed union
types.I assume the reason this is currently not supported by the language is that
union(X)
is used to specify X as the tag type,and overloading it to instead specify X as the backing type in
packed union(X)
would be confusing.Proposal
I propose the backing type to instead become the syntactic "argument" of the
packed
keyword.With this, status-quo
packed struct(Backing)
becomespacked(Backing) struct
,and
packed(Backing) union
becomes unambiguously expressible.I then secondly propose supporting
packed(Backing) union
in the language.std.builtin.Type.Union
would have to gain fieldbacking_integer: ?type
fromstd.builtin.Type.Struct
in order for
@typeInfo
and@Type
to integrate with this additional information.Potential drawbacks
This slightly reduces grep-ability for
packed struct
syntax - I'm not sure how often this comes up in workflows.If that is a concern, a mitigation against this would be to order
packed
after the container type,yielding f.e.
struct packed
,struct packed(u16)
,union packed(u12)
.The text was updated successfully, but these errors were encountered: