-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Add opaque syntax that allows declarations #6421
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
Conversation
f6eb0e9
to
5137810
Compare
This looks nice, but I have a few questions.
I use parent struct to mean the struct in which another struct, the child struct is declared, but please correct me if the nomenclature is incorrect. |
I don't think it has anything in common with structs.
C allows both opaque unions and opaque structs, but it doesn't actually matter to code which one is used, since you can't access anything from an opaque type.
Opaque types can't have fields, and any declarations have the same rules as other containers |
As mentioned in #4638 (comment), I recently started work on idiomatic zig bindings for libwayland. These bindings are still in a highly experimental state and are not complete, but nonetheless they provide a good opportunity to test out this new feature as libwayland makes heavy use of opaque pointers. My initial impressions using this feature are entirely positive: removing the need for wrapper structs to add "methods" to opaque types cleaned up and simplified things significantly. You can see the initial commit migrating to this feature here: ifreund/zig-wayland@bf06840. I intend to move forward with the development of the bindings on that branch for the time being in order to continue to test the new syntax and because the new syntax is much nicer for this use case. |
Certainly a nice addition however it looks a bit odd to me, especially Or construct it from comptime list of function pointers (do we need constants in opaque types?), and use it something like this: fn consume(self: anytype, data: []const u8) void {}
const Consumer = std.meta.Opaque(.{&consume}); |
It’s nothing like a struct though as it cannot have fields and may hide literally any type including structs, unions, etc.
That looks much more awkward to use. And yes, in my opinion opaque types should allow all kinds of Decls, not just functions. As seen in the diff I posted above I already rely on non-function Decls in my opaque types. |
@tadeokondrak consider making curly braces optional when the body of an opaque type is empty. ( |
@Rocknest Hmm, I like how that looks, but it feels confusing that a single keyword creates a new type every time it's used. |
This is already the case for Nevertheless, I'd be against making the braces optional. They aren't much pain to type and leaving them out wouldn't be worth the inconsistency/complexity IMO. |
Yeah, but that holds for |
There is nan that is not equal to self, which is far weirder behaviour if you ask me |
By the way, the test failure looks legit: https://dev.azure.com/ziglang/zig/_build/results?buildId=9322&view=logs&j=9512b82f-d185-50ea-ee23-d010bc14782f&t=ca25486c-bdcc-50c6-de82-a9ccd6b64318&l=73738 |
d308bef
to
614f108
Compare
As another sample, here's zig_clang/translate_c converted to use it: tadeokondrak@8dd2a03 |
6eacae5
to
c266a70
Compare
c266a70
to
bf4bfe5
Compare
This would be absolutely amazing if merged. Not only for C bindings, but for any case where you need to represent a handle to a data structure that does not operate in terms of fields (in the traditional sense, at least), but still needs ABI-compatible pointers: all without losing the benefits method call syntax allows! |
+1 from me. Looking through the linked wayland wrapper using the opaque fork, its really simple to reason about, and much less cluttered. I like. lol - merged, beat me to it :) |
Implements #5574 (also #4638, I had searched for "declarations" and didn't find anything).
These are not accepted proposals, so this PR might be rejected for that reason. I don't really mind.
I think it's hard to judge whether this feature is a good idea or not without experimenting with an implementation first.
Includes a zig fmt auto-fix because
@Type(.Opaque)
is no longer valid.