In structures, initialization failure is triggered before properties are set to their initial value.
However, in classes a failure is triggered after all properties have been set and delegation has taken place. Why is that?
Why can't both structures and classes trigger initialization failure at the very end of their initialization process?
UPDATE:
Here are the example codes from the Apple Swift documentation:
In the following structure example, initialization failure is triggered before any property is initialized:
struct Animal {
let species: String
init?(species: String) {
if species.isEmpty { return nil }
self.species = species
}
}
In the following class example, initialization failure is triggered after properties are set:
class Product {
let name: String!
init?(name: String) {
self.name = name
if name.isEmpty { return nil }
}
}
The documentation goes on to state:
For classes, however, a failable initializer can trigger an initialization failure only after all stored properties introduced by that class have been set to an initial value and any initializer delegation has taken place.
Why, in classes, does initialization failure occur ONLY after all properties have been set to their initial value (and delegation taken place)?