Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Zig simple runner through function pointer
const std = @import("std");
const builtin = @import("builtin");
const FnProto = switch (builtin.zig_backend) {
// workaround until we replace stage1 with stage2
.stage1 => ?fn (*Runner) anyerror!void,
else => ?*fn (*Runner) anyerror!void,
const Runner = struct {
file: std.fs.File,
pub fn init(file: std.fs.File) Runner {
return .{
.file = file,
pub fn run(runner: *Runner, func: FnProto) anyerror!void {
if (func != null)
try func.?(runner);
fn dumpPrint(runner: *Runner) anyerror!void {
try runner.file.writer().writeAll("ok\n");
test "fnproto" {
const stdout =;
var runner = Runner.init(stdout);
// doesnt work:
//var dump_print = dumpPrint(&runner);
// stage2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment