Hello,
I was wondering about failable initializers in Swift. I noticed that for structs it works as expected (at least by me), for example this is fine:
struct Test {
var someState: Int
init?(value: Int) {
if value < 0 { return nil }
someState = value // we set the value after validation
}
}But for classes swift wouldn't allow you to fail until you set internal states like so:
class Test {
var someState: Int
init?(value: Int) {
if value < 0 {
someState = 0 // this seems unneeded
return nil
}
someState = value
}
}This seems wrong. Why do I need to populate class' data with unneeded default values before I can fail the initialization?
I can see that this is valid for both Swift 1.2 and 2.0 so what am I missing here?