#How to use anytype to for a callback signature

1 messages · Page 1 of 1 (latest)

dusk canyon
#

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.

minor vector
#

it looks like youre passing checker itself to something rather than an instance of it

#

fields only exist on instances of types

dusk canyon
#

facepalm That does make sense. Let me try instantiating it

#

Yup, that was it. Rookie mistake. Thank you very much!