请考虑以下代码:
immutable struct Test {
this(int foo) { }
int opApply(int delegate(ref int) dg) {
return (0);
}
}
int main(string[] argv) {
auto set = Test(); // compiles
// auto set = Test(1); // Error: cannot uniquely infer foreach argument types
foreach (item; set) { }
return 0;
}
使用默认的无参数构造函数构建Test
结构时,代码编译正常,但是当我尝试使用任何其他构造函数时,我得到编译时错误。如果我注释掉foreach
,代码将编译。如果我注释掉immutable
,代码也会编译。
这种行为的原因是什么以及如何修复?
答案 0 :(得分:3)
实际上,至少使用DMD版本2.059,它不能使用任何构造函数进行编译(在Windows 7和FreeBSD上测试)。
这个原因应该是相当明显的。通过使结构(或类)不可变,您只需将不可变应用于该结构(或类)的每个成员。但是,构造函数不会变为不可变。也就是说,当你声明immutable struct Test
时,你已经有效地完成了以下任务:
struct Test {
this(int foo) { }
immutable int opApply(int delegate(ref int) dg) {
return (0);
}
}
注释foreach循环允许代码编译,因为foreach正在寻找没有immutable
声明的opApply方法。
根据您要执行的操作,您可以简单地创建结构final
而不是immutable
,或者,如果您希望保持大部分结构不可变...
struct Test {
// Anything that needs to be mutable should go up here
int opApply(int delegate(ref uint) dg) {
return 0;
}
// Anything put in this block is immutable
immutable {
this(int foo) { }
}
}