我想要“赋值或”和“赋值和”运算符。根据Swift Standard Library Operators Reference,这些操作符是在标准库中定义的。实现|| =和&& =操作符:运算符实现没有匹配运算符声明
我试图执行这些运营商Bool
值:
func ||= (inout lhs: Bool, rhs: Bool) {
lhs = lhs || rhs
}
func &&= (inout lhs: Bool, rhs: Bool) {
lhs = lhs && rhs
}
但是编译器会抱怨:operator implementation without matching operator declaration
这可以通过定义运营商固定:
infix operator ||= { associativity right precedence 90 }
infix operator &&= { associativity right precedence 90 }
但我不当然,这是正确的做法。为什么标准库中的定义不起作用?此外,我注意到,根据运营商的参考标准库没有任何类型的这些运营商的实施。这是为什么?这是一个疏忽还是有意的?
有趣。但是你的实现对我来说看起来不错。 –
@ J.Wang是的,它似乎工作正常,但我想知道如果重新实现这些运营商是正确的事情做 –
@dfri啊,我明白了,谢谢!你会发布这个答案吗? –