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. }
  1. $ zig test test.zig
  2. ./docgen_tmp/test.zig:6:9: error: expected type '*Derp', found '*Wat'
  3. bar(w);
  4. ^
  5. ./docgen_tmp/test.zig:6:9: note: pointer type child 'Wat' cannot cast into pointer type child 'Derp'
  6. bar(w);
  7. ^