opaque

opaque {} declares a new type with an unknown (but non-zero) size and alignment. It can contain declarations the same as structs, unions, and enums.

This is typically used for type safety when interacting with C code that does not expose struct details. Example:

test.zig

  1. const Derp = opaque {};
  2. const Wat = opaque {};
  3. extern fn bar(d: *Derp) void;
  4. fn foo(w: *Wat) callconv(.C) void {
  5. bar(w);
  6. }
  7. test "call foo" {
  8. foo(undefined);
  9. }

Shell

  1. $ zig test test.zig
  2. docgen_tmp/test.zig:6:9: error: expected type '*test.Derp', found '*test.Wat'
  3. bar(w);
  4. ^
  5. docgen_tmp/test.zig:6:9: note: pointer type child 'test.Wat' cannot cast into pointer type child 'test.Derp'
  6. docgen_tmp/test.zig:2:13: note: opaque declared here
  7. const Wat = opaque {};
  8. ^~~~~~~~~
  9. docgen_tmp/test.zig:1:14: note: opaque declared here
  10. const Derp = opaque {};
  11. ^~~~~~~~~
  12. docgen_tmp/test.zig:4:18: note: parameter type declared here
  13. extern fn bar(d: *Derp) void;
  14. ^~~~~
  15. referenced by:
  16. test.call foo: docgen_tmp/test.zig:10:5
  17. remaining reference traces hidden; use '-freference-trace' to see all reference traces