Is it possible to mutate a string in Swift such that it can be proven to modify the original value?

亡梦爱人 提交于 2019-12-02 02:41:45

问题


Start with the claim that Swift makes that strings are 'mutable', Are Swift "mutable" strings really mutable, or are they just like Java strings?, but proceeding with a generally-accepted (and non-Swift) definition of mutability - ie. strictly value immutability, without consideration of bindings

Is it possible to actually mutate a String value such that this prints 'true'?

var str = "Mutate me!"

let a1 = (unsafeAddressOf(str))
// some 'mutating operation'
let a2 = (unsafeAddressOf(str))

print(a1 == a2)

I am not interested in 'structure types' or delayed copy semantics. This question is about if a string value can be modified, from within Swift - although it would be also be interesting to see if such could be mutated by other .. devious means. (If not it is hogwash to even bother discussing value vs reference types instead of pure mutability considerations.)

I am aware of "mutating" methods but disagree with those as 'mutating' the string value. In the example above, let should be substitutable in the final program. If let cannot be substituted; or the object can otherwise not be proven to be the same, then mutability has not been shown.

来源:https://stackoverflow.com/questions/32637688/is-it-possible-to-mutate-a-string-in-swift-such-that-it-can-be-proven-to-modify

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!