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
const Derp = opaque {};
const Wat = opaque {};
extern fn bar(d: *Derp) void;
fn foo(w: *Wat) callconv(.C) void {
bar(w);
}
test "call foo" {
foo(undefined);
}
Shell
$ zig test test.zig
docgen_tmp/test.zig:6:9: error: expected type '*test.Derp', found '*test.Wat'
bar(w);
^
docgen_tmp/test.zig:6:9: note: pointer type child 'test.Wat' cannot cast into pointer type child 'test.Derp'
docgen_tmp/test.zig:2:13: note: opaque declared here
const Wat = opaque {};
^~~~~~~~~
docgen_tmp/test.zig:1:14: note: opaque declared here
const Derp = opaque {};
^~~~~~~~~
docgen_tmp/test.zig:4:18: note: parameter type declared here
extern fn bar(d: *Derp) void;
^~~~~
referenced by:
test.call foo: docgen_tmp/test.zig:10:5
remaining reference traces hidden; use '-freference-trace' to see all reference traces