Jack-Ji / jok

A minimal 2d/3d game framework for @ziglang.
MIT License
172 stars 6 forks source link

allow defer in jd2 and jd3? #10

Closed Ruulul closed 6 months ago

Ruulul commented 6 months ago

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:

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.

Jack-Ji commented 6 months ago

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.