Perhaps there should be a rule that a noncopyable struct
(I refuse to type the preceding @
) with a deinit
must include an explicit deinit self
on all codepaths that follow complete initialization. Then there’s no ambiguity about when deinit
occurs:
noncopyable struct DatabaseHandle {
var dbConn: DatabaseConnection
init(hostname: String, port: Int, username: String, password: String) throws {
guard let dbConn = DatabaseConnection.open(to: hostname, port: port)
else {
throw ConnectionError()
}
// self is fully initialized at this point
guard dbConn.authenticate(as: username, password: password)
else {
deinit self
throw AuthenticationError()
}
}
deinit {
dbConn.close()
}
}