Jack-Ji/jok

allow defer in jd2 and jd3?

Closed this issue · 1 comments

Using defer on pairs of setup and cleanup is a common zig idiom, but since the end() functions can error, its impossible right now.

Suggested refactor:

  • make an error union variable;
  • rewrite the try expressions to catch, save the error and return;
  • make begin() try the end error as the first thing;

so it would be something like

+ var end_error: anyerror!void = {};
...
pub fn begin(opt: BeginOpt) !void {
    + try end_error;
    ....
}
...
-pub fn end() !void {
+pub fn end() void {
-    try [expr];
+    [expr] catch |e| return end_error = e;
}

Gonna write down a PR, just thought it would be good to open an issue first.

Hi @Ruulul, thanks for the PR. Believe it or not, j2d.end and j3d.end used to be void functions. I don't quite remember why I changed them, maybe some errors were too important to be ignored at the time. Anyway, both functions have been refactored many times after then, I think it's ok to simply make them void functions again.