Swift: support mutating construct for classes
| Originator: | owensd |
|
| Number: | rdar://17279012 |
Date Originated: | 11-Jun-2014 04:55 PM |
| Status: | Open |
Resolved: | |
| Product: | Developer Tools |
Product Version: | |
| Classification: | Enhancement |
Reproducible: | Always |
Please consider supporting the mutating construct that is already supported for structs onto classes. This will provide us with everything we need to create truly const classes as well as avoid the unfortunate pseudo-constant struct/class composition problems.
As an example, see this program:
class CPerson {
var name : String
init(name : String) { self.name = name }
}
struct SPerson {
var age : Int
var person : CPerson
mutating func haveBirthday() {
println("Happy birthday!")
age++
}
}
let david = SPerson(age: 32, person: CPerson(name: "David"))
var frank = SPerson(age: 35, person: CPerson(name: "Frank"))
//david.age = 23 // compiler error, as it should
david.person.name = "Sally" // this SHOULD be a compiler error, but classes do not support 'mutating' or const in general
david.person.name // prints "Sally"
frank.age = 23
frank.person.name = "David"
frank.person.name // prints "David"
If classes supported mutating, the let keyword would provide const protection down the members of the struct avoiding being able to change the underlying values of the struct.
Duplicates
Comments
ag9zfm9wZW5yYWRhci1ocmRyEgsSBVJhZGFyGICAgMDoyr8JDA
Please note:
Reports posted here will not necessarily be seen by Apple.
All problems should be submitted at bugreport.apple.com before they are posted here.
Please only post information for Radars that you have filed yourself, and please do
not include Apple confidential information in your posts. Thank you!