是否可以像在C++中的static_assert一样在Swift中编译一个编译时断言?也许某种方式利用泛型的类型约束来强制编译器中断?
我是 d3.js 的新手,并尝试复制类似于选择连接的东西,支持使用嵌套选择进行更新。我在早期版本中看到过这个问题,并带有明确的enter/exit并尝试对其进行调整。但是,我无法完全弄清楚这段代码的内部绑定。例如,顶部迭代的n-1小写字母在后续间隔中是粉红色的,而不是在迭代中不变的字母。代码笔
const main = d3.select("main");
function updateOld() {
content = randomUpper().map(u => {
return {
header: u,
list: randomLower()
};
});
let outer = main.selectAll("div")
.data(content, d => d.header)
outer.exit().remove();
const outerEnter = outer.enter()
.append("div")
.style("color", "green")
.text(d => d.header);
outer = outerEnter.merge(outer.style("color", "gray"));
inner = outer.selectAll("h6")
.data(d => d.list);
inner.exit().remove();
inner.enter()
.append("h6")
.style("color", "blue")
.merge(inner.style("color", "pink"))
.text(d => d);
}
setInterval(updateOld, 2000);
updateOld();
function …Run Code Online (Sandbox Code Playgroud)我试图使用repeatedValues初始化程序初始化包含可选值的数组,我惊讶地发现以下代码无法编译
let a: Int?[] = Int?[](count: 10, repeatedValue:nil)
// error - Value of Int?[]? not unwrapped; did you mean to use '!' or '?'?
Run Code Online (Sandbox Code Playgroud)
有趣的是类型签名Int?[]?,例如可选Array的可选项Int.这感觉就像一个错误,但也许我对语法缺少了一些东西.我已经查看了一些语言参考但尚未找到答案.
更明确的Array<Int?>类型初始化程序按预期工作
let b: Int?[] = Array<Int?>(count: 10, repeatedValue:nil)
// compiles fine
Run Code Online (Sandbox Code Playgroud)
有没有其他人遇到这个并且可以解决一些问题?
编辑
结合非可选类型的额外工作示例来突出显示故障
let c: Int[] = Int[](count: 10, repeatedValue:0)
// non-optional shorthand works fine
class D { var foo = 1 }
let d: D[] = D[](count:10, repeatedValue:D())
// custom class works fine using the …Run Code Online (Sandbox Code Playgroud)