I'm attempting to implement a stateful callback API where the caller can provide both a stateful type and a function to invoke as a callback. The stateful type should be passed as the first argument to the callback, along with some data.
I've attempted to write it like this:
const checker = struct {
insertCounter: usize = 0,
fn check(self: anytype, op: Operation) void {
if (op.kind != OpKind.InsertAfter) return;
if (self.insertCounter == 0) {
try testing.expect(op.id.isZero());
}
self.insertCounter += 1;
std.log.debug("replace operation {}", .{op});
}
};
However, I get a confusing build error:
root.zig:745:21: error: struct 'root.test.Document replace.checker' has no member named 'insertCounter'
if (self.insertCounter == 0) {
~~~~^~~~~~~~~~~~~~
root.zig:741:21: note: struct declared here
const checker = struct {
^~~~~~
referenced by:
replace__anon_25230: root.zig:353:21
test.Document replace: root.zig:753:18
I thought that duck typing would successfully resolve that field. It definitely seems to exist. What am I doing wrong here?
Zig 0.14.1.
That does make sense. Let me try instantiating it