Swift 为什么在使用try时可以使用inout参数对let常量进行变异?
这将无法编译,并出现以下错误: 不能将不可变值“self.constantValue”传入输出 然而,这将编译并实际改变let常量Swift 为什么在使用try时可以使用inout参数对let常量进行变异?,swift,pointers,immutability,inout,Swift,Pointers,Immutability,Inout,这将无法编译,并出现以下错误: 不能将不可变值“self.constantValue”传入输出 然而,这将编译并实际改变let常量 class Test { let constantValue: String = "" init() { try? Test.makeABC(&constantValue) } static func makeABC(_ string: inout String) throws { str
class Test {
let constantValue: String = ""
init() {
try? Test.makeABC(&constantValue)
}
static func makeABC(_ string: inout String) throws {
string = "ABC"
}
}
有人知道为什么会这样,以及这是否是有意的行为吗
我提交了一个bug,对我来说它看起来像个bug。@MartinR有趣的是,这种行为仅限于
init
。将try?
移动到一个普通方法,编译器也会拒绝该方法。修复了另一个测试的打字错误,祝贺您找到了一个很好的边缘案例!我们总是喜欢这样。做得好。
class Test {
let constantValue: String = ""
init() {
try? Test.makeABC(&constantValue)
}
static func makeABC(_ string: inout String) throws {
string = "ABC"
}
}