如何try-catch在 Zig 中实现经典的错误处理?
例如。如何解决这个错误并且只有append在没有错误发生时才执行?
var stmt = self.statement() catch {
self.synchronize(); // Only execute this when there is an error.
};
self.top_level.statements.append(stmt); // HELP? This should only be executed when no error
// ...
fn synchronize() void {
// ...implementation
}
fn statement() SomeError!void {
// ...implementation
}
Run Code Online (Sandbox Code Playgroud)
如果可能,请显示上述代码的修改版本。
当我将一个函数传递给另一个函数时,出现以下错误?
const std = @import("std");
const St = struct { a: usize };
fn returnFunc(print: fn (str: []const u8, st: St) void) void {
print("Hello", St{ .a = 1 });
}
fn toTest(str: []const u8, st: St) void {
std.debug.print("{s}: {d}\n", .{str, st.a});
}
pub fn main() !void {
returnFunc(toTest);
}
Run Code Online (Sandbox Code Playgroud)
返回以下错误:
error: parameter of type 'fn([]const u8, main.St) void' must be declared comptime
Run Code Online (Sandbox Code Playgroud)
机器详细信息: Zig 版本:0.10.0-dev.4588+9c0d975a0 M1 Mac、MAC OS Ventura
我希望能够管理我的 SvelteKit 应用程序的历史记录,同时确保 SvelteKit 的整个路由系统不会受到任何影响。
就像是:
function routeToPage(route: string) {
router.push(`/${route}`) // equivalent of this function
}
Run Code Online (Sandbox Code Playgroud) 如何在 Zig 编译时连接以下长度已知的字符串?
const url = "https://github.com/{}/reponame";
const user = "Himujjal";
const final_url = url + user; // ??
Run Code Online (Sandbox Code Playgroud)