如何解决为什么Zig中用户定义的类型需要为const?
如果需要在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);
}
但是它可以同时以var
和const
两种方式进行初始化,所以为什么类型声明只需要struct实例是否为const
时才需要一个常量关键字?
解决方法
之所以必须为const
,是因为未定义根范围中的求值顺序,并且因为类型type
的变量只能存在于编译器中(没有内存表示,编译器无法产生它的二进制表示)。但是您可以在其他范围内使用var
:
comptime {
var T = struct { value: u64 };
const x: T = .{ .value = 3 };
T = struct { ok: bool };
const y: T = .{ .ok = true };
@compileLog(x.value); // <- 3
@compileLog(y.ok); // <- true
}
在其余的答案中,我会详细解释。
const
const Arith = struct {...};
创建一个推断类型的常量。在这种情况下,变量Arith
的类型为type
:
const Arith = struct {...};
comptime {
@compileLog(@TypeOf(Arith)); // <- type
}
这与将变量声明为:
const Arith: type = struct {...};
Var
您还可以使用var
示例:
comptime {
var Arith = struct {...};
}
comptime {
var Arith: type = struct {...};
}
fn main() !void {
comptime var Arith = struct {...};
}
fn main() !void {
var Arith: type = struct {...};
}
fn main() !void {
comptime var Arith: type = struct {...};
}
由于是变量,您可以对其进行修改:
comptime {
var T = u64;
T = bool;
@compileLog(T); // <-- bool
}
Comptime类型
有些类型只能在编译器中使用,例如:type
或具有字段anytype
或其他comptime类型的结构。
对于type
,这使编译器将var x: type
解释为comptime var x: type
。
然后,考虑以下代码:
var T = struct { value: u64 }; // <- Compiler error
comptime {
const x: T = .{ .value = 3 };
}
error: variable of type 'type' must be constant
由于未定义根范围中的求值顺序,因此编译器会强制在二进制文件内创建一个全局变量,但是类型type
没有内存表示形式。因此,编译器会引发错误。
由于Arith
的类型为type
,因此必须将其声明为常量,因为编译器希望它是常量。可以通过将类型声明更改为此并运行程序来进行检查
var Arith = struct {
x: i32,y: i32,fn add(self: *Arith) i32 {
return self.x + self.y;
}
};
这将导致错误 error: variable of type 'type' must be constant
在Zig中,我们还需要在名称前加上const
或var
,否则将其视为无效令牌。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。