# How to use callbacks with run-time arguments?

**URL:** <https://ziggit.dev/t/how-to-use-callbacks-with-run-time-arguments/2449>\
**Category:** Help\
**Tags:** language\
**Created:** [December 13, 2023, 6:46am UTC](https://ziggit.dev/t/how-to-use-callbacks-with-run-time-arguments/2449 "2023-12-13T06:46:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zigo](https://ziggit.dev/user_avatar/ziggit.dev/zigo/32/2414_2.png) [@zigo](https://ziggit.dev/u/zigo)\
**Post date:** [December 13, 2023, 6:46am UTC](https://ziggit.dev/t/how-to-use-callbacks-with-run-time-arguments/2449/1 "2023-12-13T06:46:42Z")

</div>

Here is an example in which a callback with compile-time argument is used.

```zig
const std = @import("std");

fn count_until(data: []const u8, comptime untilFn: fn (u8) bool) usize {
    var index: usize = 0;
    while (index < data.len) {
        const c = data[index];
        if (untilFn(c)) {
            return index;
        }
        index += 1;
    }

    return data.len;
}

fn count_until_not_char(data: []const u8, comptime c: u8) usize {
    const T = struct {
        fn isNot(r: u8) bool {
            return r != c;
        }
    };

    return count_until(data, T.isNot);
}

pub fn main() !void {
    const data = "AAAA, world!";
    std.debug.print("{}\n", .{count_until_not_char(data, 'A')}); // 4
}

```

But I failed to find a way to use callback with run-time arguments. One of my (failed) attempts is shown below.

```zig
fn count_until_not_char(data: []const u8, c: u8) usize {
    const T = struct {
    	c: u8,
    	
        fn isNot(r: u8) bool {
            return r != c;
        }
    };
    
    const t = T{.c = c};

    return count_until(data, t.isNot); // error: no field named 'isNot' ...
}

```

It looks Zig doesn’t support using object methods as function values and it looks function parameters must be comptime in Zig.

How do you implement such needs?

---

<div class="post-metadata">

**Author:** ![zigo](https://ziggit.dev/user_avatar/ziggit.dev/zigo/32/2414_2.png) [@zigo](https://ziggit.dev/u/zigo)\
**Post date:** [December 13, 2023, 7:32am UTC](https://ziggit.dev/t/how-to-use-callbacks-with-run-time-arguments/2449/2 "2023-12-13T07:32:47Z")

</div>

Never mind, just found a way.

```zig
const std = @import("std");

fn count_until(data: []const u8, until: anytype) usize {
    var index: usize = 0;
    while (index < data.len) {
        const c = data[index];
        if (until.check(c)) {
            return index;
        }
        index += 1;
    }

    return data.len;
}

const UntilNotChar = struct {
	c: u8,
	
	fn check(self: @This(), r: u8) bool {
		return r != self.c;
	}
};

const UntilChar = struct {
	c: u8,
	
	fn check(self: @This(), r: u8) bool {
		return r == self.c;
	}
};

pub fn main() !void {
    const data = "AAAA, world!";
    
    std.debug.print("{}\n", .{count_until(data, UntilNotChar{.c = 'A'})}); // 4
    std.debug.print("{}\n", .{count_until(data, UntilChar{.c = '!'})}); // 11
}

```

Do you have simpler ways?

---

<div class="post-metadata">

**Author:** ![permutationlock](https://ziggit.dev/user_avatar/ziggit.dev/permutationlock/32/9958_2.png) [@permutationlock](https://ziggit.dev/u/permutationlock)\
**Post date:** [December 13, 2023, 7:35am UTC](https://ziggit.dev/t/how-to-use-callbacks-with-run-time-arguments/2449/3 "2023-12-13T07:35:27Z")

</div>

Yeah, if want to have runtime state associated with a function you will need to pass that state around alongside the function (or use a [static variable](https://ziglang.org/documentation/master/#toc-Static-Local-Variables), but I usually try to avoid that).

Your version seems good. Another version similar to what you originally posted and inspired by the implementation of [`std.sort.insertion`](https://github.com/ziglang/zig/blob/a38af5f542199378483aba9f4598634ebf012b7b/lib/std/sort.zig#L16) is:

```zig
const std = @import("std");

fn count_until(
    data: []const u8,
    context: anytype,
    comptime untilFn: fn (@TypeOf(context), u8) bool,
) usize {
    var index: usize = 0;
    while (index < data.len) {
        const c = data[index];
        if (untilFn(context, c)) {
            return index;
        }
        index += 1;
    }

    return data.len;
}

fn count_until_not_char(data: []const u8, c: u8) usize {
    const T = struct {
        c: u8,

        pub fn isNot(self: @This(), r: u8) bool {
            return r != self.c;
        }
    };

    const t = T{ .c = c };
    return count_until(data, t, T.isNot);
}

pub fn main() !void {
    const data = "AAAA, world!";
    std.debug.print("{}\n", .{count_until_not_char(data, 'A')}); // 4
}

```

---

<div class="post-metadata">

**Author:** ![dude\_the\_builder](https://ziggit.dev/user_avatar/ziggit.dev/dude_the_builder/32/557_2.png) [@dude\_the\_builder](https://ziggit.dev/u/dude_the_builder)\
**Post date:** [December 13, 2023, 12:26pm UTC](https://ziggit.dev/t/how-to-use-callbacks-with-run-time-arguments/2449/4 "2023-12-13T12:26:40Z")

</div>

From what I’ve seen out in the wild, this technique is also common in many C libraries, where they use `*void` where Zig uses `anytype`.

---

<div class="post-metadata">

**Author:** ![Sze](https://ziggit.dev/user_avatar/ziggit.dev/sze/32/496_2.png) [@Sze](https://ziggit.dev/u/Sze)\
**Post date:** [December 13, 2023, 6:33pm UTC](https://ziggit.dev/t/how-to-use-callbacks-with-run-time-arguments/2449/5 "2023-12-13T18:33:05Z")

</div>

I think an important question here is whether the set of possible “callbacks” is closed or open, meaning: “are all the possible kinds of callbacks known at compile time and they only differ by runtime data?”. If that is the case I would ditch the callbacks and use a tagged union instead:

```zig
const std = @import("std");

const Compute = union(enum) {
    found: u8,
    not_found: u8,
    sum_at_least: struct {
        target: usize,
        start: usize = 0,
    },

    fn compute(self: *@This(), r: u8) bool {
        switch (self.*) {
            .found => |f| return f == r,
            .not_found => |n| return n != r,
            .sum_at_least => |*u| {
                u.start += r;
                return u.start >= u.target;
            },
        }
    }
};

fn countUntil(data: []const u8, c: *Compute) usize {
    var index: usize = 0;
    while (index < data.len) {
        const r = data[index];
        if (c.compute(r)) {
            return index;
        }
        index += 1;
    }

    return data.len;
}

pub fn main() !void {
    const seed: u64 = @intCast(std.time.nanoTimestamp());
    std.debug.print("seed: {}\n", .{seed});

    var gen = std.rand.DefaultPrng.init(seed);
    const rng = gen.random();

    const data = "AAAA, world!";
    var c: Compute = switch (rng.uintLessThan(u8, 3)) {
        0 => .{ .found = '!' },
        1 => .{ .not_found = 'A' },
        2 => .{ .sum_at_least = .{ .target = 800 } },
        else => unreachable,
    };
    std.debug.print("compute: {}\n", .{c});

    std.debug.print("{}\n", .{countUntil(data, &c)});
}

```

If the set is open and you need to choose at runtime, you can define an interface that uses type erasure like `std.mem.Allocator` instead of the tagged union.

If the choice can be made at comptime you could use a comptime enum (possibly associated with a runtime payload/tagged union) like here: [Tagged union with comptime tag - #2 by Sze](https://ziggit.dev/t/tagged-union-with-comptime-tag/984/2)

I think the other answers are also valid and useful, I just prefer union if it is enough to do the job. I enjoy that it can be used to basically limit the scope of what needs to be considered, in my example you simply see it takes a `*Compute` you look at compute and can see all the cases, which also makes it easier to evaluate whether `countUntil` works correctly for all cases. With open solutions you don’t get that. So I guess my 2 cents are, don’t use too generic solutions, staying specific has its benefits.

---

<div class="post-metadata">

**Author:** ![zigo](https://ziggit.dev/user_avatar/ziggit.dev/zigo/32/2414_2.png) [@zigo](https://ziggit.dev/u/zigo)\
**Post date:** [December 14, 2023, 6:12am UTC](https://ziggit.dev/t/how-to-use-callbacks-with-run-time-arguments/2449/6 "2023-12-14T06:12:02Z")

</div>

Thanks all for the suggested ideas. Learned much new Zig knowledge.
