[swift-dev] Initialization of default values

Matthew Johnson matthew at anandabits.com
Thu Dec 10 12:13:27 CST 2015

Thanks Joe.  It sounds like in both current state and future state the compiler synthesizes additional arguments to the initializers for the type.  Is that correct?

Doug and Jordan, if you’re able to offer any additional insights that would be much appreciated as well.  I want to make sure my proposal aligns well with how you’re handling this.

This is some code showing my understand of how you describe current and future state.  Can you confirm if this is correct?

struct S {
    let d: Double
    let s: String = "default"
    // actual compiler generated signature is init(d: Double, s: String)
    init(d: Double) {
        // compiler generated initialization of s
        // self.s = s
        self.d = d
    // current state compiler generated default value function
    static func sDefault() -> String { return "default" }

// current state: actual compiler generated call is S(d: 1, s: S.sDefault())
// future state: actual compiler generated call is S(d: 1, s: "default")
let s = S(d: 1)


> On Dec 10, 2015, at 10:28 AM, Joe Groff <jgroff at apple.com> wrote:
>> On Dec 9, 2015, at 3:28 PM, Matthew Johnson via swift-dev <swift-dev at swift.org> wrote:
>> I am working on a proposal for flexible memberwise initialization and would like to understand how Swift currently handles initialization of members that have a default specified in the declaration when an initializer does not explicitly initialize them.  Can someone familiar with this provide a brief summary or point me in the right direction to learn more (either documentation or compiler code).
> This is under flux. Currently the compiler emits a default argument generator function for each defaulted argument, and emits a corresponding call at any call site that uses the default argument. The idea behind this was that the value of the argument could be changed without breaking ABI, but in practice this has been problematic, and constrains resilience in other undesirable ways. We're planning to move to a more C++-like model, where the default argument expression is instantiated at each call site. I think Doug and Jordan would be the best ones to discuss both the current model and our planned final design.
> -Joe

More information about the swift-dev mailing list