Just like how Rust offers todo!()
or how Ocaml has let func = "Failure "Unimplemented""
is there a way to write a function in zig that doesn't have the body implemented and doesn't raise any warnings and errors after being compiled?
For now I just write it with a print for e.g.
pub fn do_something(self: *MyType) void {
std.debug.print("TODO %d", self.field);
}
The standard library uses the @compileError
builtin to indicate deprecations. You could use this builtin with a "TODO" string:
fn do_something(self: *MyType) void {
_ = self;
@compileError("TODO: not yet implemented");
}
Due to Zig's lazy compilation, a compile error will only be thrown if do_something
is actually used. You'll still need to discard unused arguments, though. You can avoid this by dropping the function signature and defining do_something
as a constant (see std.mem.tokenize
).
A slight difference between this and Rust is that this fails to compile, whereas Rust panic
s. If you prefer panicking over compilation errors, you may use @panic
(see std.Target.cTypeBitSize
):
fn do_something(self: *MyType) void {
_ = self;
@panic("TODO: not yet implemented");
}
A benefit to using @compileError
over @panic
is that ZLS will catch @compileError
at compile time and highlight the symbol as deprecated; see PR #1764 for more info. However, if the code is considered for compilation, it will fail. This makes @panic
more flexible and appropriate for the use-case, IMO.