Currently, if you want to modify a struct's default memberwise initialiser you have to write one out manually/auto-generate one via the IDE.
struct ImAStruct {
let aVariable: String
// This is inferred and doesn't need writing manually
init(aVariable: String) {
self.aVariable = lovelyVariable
}
}
For example:
struct ImAStruct {
let aVariable: String
// This cannot be inferred due to the modification
init(aVariable: String) {
self.aVariable = aVariable
if aVariable.contains("something") {
print("Has something")
}
}
}
Could be something such as:
struct ImAStruct {
let aVariable: String
inferred init {
if aVariable.contains("something") {
print("Has something")
}
}
}
In an inferred init
, you wouldn't need to manually assign variables/constants. This is purely for convenience and cleaner looking code.
The inferred init
's properties/call would always be the same as if an initialiser wasn't written for the struct - i.e. not polymorphic, this can be done via extension.
This does have similarity to SE-0018
, however, that proposal was still specifying parameters in the init
which could lead to ordering issues etc. inferred
could be clearer than memberwise
.
The inferred init
wouldn't necessarily allow changes to scope of the init
without a subsequent proposal.
A major use case would be with SwiftUI Views, where function calls etc within the init
, would be needed in a Struct as opposed to a Class.
Thoughts welcome (or removal if this has been raised already).