在 Zig 0.9 中,我需要一个类型为 的文字表达式void,用作 的上下文参数std.sort.sort,以便我的lessThan函数签名在语义上准确。有一些吗?
我尝试了这些但没有效果:
const ek = @import("std").io.getStdOut().writer();
test "void" {
const kandidati = .{ type, u0, .{}, void, null, undefined };
inline for (kandidati) |k|
try ek.print("{}, ", .{@TypeOf(k)});
try ek.print("\n", .{});
}
Run Code Online (Sandbox Code Playgroud)
给予
Test [0/1] test "void"... type, type, struct:31:37, type, @Type(.Null), @Type(.Undefined),
All 1 tests passed.
Run Code Online (Sandbox Code Playgroud)
我不想使用像const v: void = undefined;;这样的虚拟变量 这太冗长了。
作为参考,使用带有类型参数的函数void作为上下文参数,会给出如下错误消息std.sort.sortlessThanvoid
error: expected type 'fn(type,anytype,anytype) anytype', found 'fn(void, Type1, …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) 例如,外部 C 库返回包含空终止字符串的固定大小缓冲区。
比较这两个缓冲区的 strcmp() 等效项是什么?std.mem.eql失败是因为它们的大小不同。
var string1: [1024]u8 = undefined;
@memset(&string1, 0);
@memcpy(string1[0..3], "foo");
var string2 = "foo";
var result = std.mem.eql(u8, &string1, string2);
try std.testing.expect(result);
Run Code Online (Sandbox Code Playgroud) 检查 zig 中是否存在文件的最简单方法是什么?
作为构建过程的一部分,我需要避免覆盖已经存在的文件。
我见过std.fs.access,但我不确定是否有更简单的方法。
Zig 编程语言提倡“类型即值”的概念,这对我来说似乎是一个非常强大的概念,我想知道还有哪些其他编程语言支持这个概念。
我知道,例如,C++ 模板可用于将类型传递给函数,但类型仍然与 C++ 中的值不同。
如果我需要在 Zig 中声明一个结构,我必须在它前面加上前缀const
const Arith = struct {
x: i32,
y: i32,
fn add(self: *Arith) i32 {
return self.x + self.y;
}
};
test "struct test" {
var testArith = Arith{
.x = 9,
.y = 9,
};
expect(testArith.add() == 18);
}
Run Code Online (Sandbox Code Playgroud)
但它可以用两种方式初始化var,那么const为什么类型声明需要一个常量关键字,而只关心结构体的实例const是否存在呢?