Most of us already seen classic reserved0, reverved1, reserved23 or even unknown1 etc
The thing is, that in many scenarios when it comes to extern structs with C-ABI interop we do not implement the struct fully or in some cases we can’t fully implement the struct, so at the end we end-up with struct full of reserved or unknown fields that has numerical sufix, since we already have a discard syntax and we also do have non-exhaustive enum, why not adding padding syntax
to structs, might even work pretty well with bitpacked struct where we can force the packed struct to fill up the backing integer explicitely, anyway
Idea is simple, any unknown or reserved0 field would became general padding using underscore syntax
pub const Interop = extern struct {
_: *anyopaque, // It might look like a pointer but we don't know what is supposed to point at
my_well_known_field: SomeType,
_: [24]u8, // Let's say we also know there is an offset of 24 unknown bytes, se we can use this padding again
my_other_well_know: BestType,
};
Since i quote:
why not adding padding syntax to structs
Here are some issues that come with it, and would need proper investigation and discussion about
-
my_struct._ = 5This is more of personal subjective opionion, but accessing padding would be strictly disallowed as it doesn’t specify what padding it refers to, (What if i want to use the padding value? Then why did u use padding in the first place duh? Also you can always cast the pointer type and access what you want) -
@typeInfo(Interop).@"struct".fieldsWould now return slice where padding field name might and might not repeat multiple times, in this case not a big deal. Most of the comptime accessing this information does some kind of serialization, and that not the case for majority of interop structs that are passed between different systems.
(PS: After more thoughts, i realized even simple formatting in std.debug.print wouldn’t work here properly and would have to skip these padding fields, but also doesn’t seems to be a big issue) -
my_struct = .{._ = 5}Would also be disallowed, in most cases you do not initialize structure you don’t fully know, so compiler can just report and disallow initialization for this structs (my_struct: MyStruct = undefinedwould be still allowed)
Tell me what do you think, also compiler would have to emit some unique symbols for each padding, but shouldn’t be big of an issue
Well my point is, lets discuss it rather sooner than later, that’s also reason i choose to bring this up, i seen a GitHub proposal issue where even this kind of proposal was slightly mentioned, but it main topic was discarded params proposal fn tadaa(_: type) and that end up being in lang and then the issue was closed.
So even if its good or bad idea, we can argue about that here now
I didn’t want to open issue on codeberg as this is more controversial and theoretical and myself is not sure if its the right way to go. That’s also why i want to hear your takes, Thanks