Are sentinels only intended for null terminators?

Sentinel-terminated anything is mostly useful for interoperating with C APIs, since if you’re dealing with Zig APIs, then regular ol’ slices are better in most situations.

So the usefulness of non-0 sentinels would be dependent on the C APIs you are interacting with. For example, the Lua C API has some functions that take an array of luaL_Reg structs where:

Any array of luaL_Reg must end with a sentinel entry in which both name and func are NULL.

In C, that looks like:

static const luaL_Reg luv_async_methods[] = {
  {"send", luv_async_send},
  {NULL, NULL}
};

In Zig, that could be expressed as something like (untested, might have the syntax here slightly wrong):

const luaL_RegTerminator = c.luaL_Reg{ .name = null, .func = null };
const luv_async_methods = [_:luaL_RegTerminator]c.luaL_Reg{
    .{ .name = "send", .func = luv_async_send },
};
10 Likes