func zero() -> Int {
print("hello")
return 0
}
struct X {
var y = zero()
init(y: Int) {
self.y = y
}
}
let x = X(y: 10) // hello
This behavior does not occur with memberwise initializers because they are lowered differently, but user defined initializers all have this same behavior.
This is a somewhat complicated issue and I plan to write a much longer topic on the forums discussing this and getting some feedback there. I'll post the link here when I get done writing it.
Additional Detail from JIRA
| | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug | |Assignee | @Azoy | |Priority | Medium | md5: db7359379f23291a48589aff8b172323Issue Description:
During SE-0242 (https://forums.swift.org/t/se-0242-synthesize-default-values-for-the-memberwise-initializer/20618/1), there was discussion about how the compiler currently treats initializers for structs and classes.
This behavior does not occur with memberwise initializers because they are lowered differently, but user defined initializers all have this same behavior.
Accepting rationale stated that the core team felt this behavior was a bug
(https://forums.swift.org/t/se-0242-synthesize-default-values-for-the-memberwise-initializer/20618/98).
This is a somewhat complicated issue and I plan to write a much longer topic on the forums discussing this and getting some feedback there. I'll post the link here when I get done writing it.