SE-0242: Synthesize default values for the memberwise initializer

Forum post here: User defined initializers always call member initializers

I strongly disagree that this is a bug. Initial value expressions should not have side effects, ideally, and if they do, it is easier to reason about them if we just say that they are always executed at the top of the initializer, than if where and when they're executed depends on the DI analysis.

To put it another way, initial value expressions are simple syntax sugar that you can eliminate by writing out the initialization by hand in each initializer; if your initial value expressions have side effects or don't always need to run for other reasons, it is best to avoid them altogether and write out the initializer body explicitly.
y

1 Like

How is this handled for let members? If a stored let property has a default value and it can be set in an initializer, then it's set only once, right? var properties could be set to use a similar rule.

let properties that have default values cannot be reassigned in an initializer.

struct Test { 
   let id = 10 
   init(id: Int) { 
       self.id = id 
   } 
 } 
error: repl.swift:8:17: error: immutable value 'self.id' may only be initialized once
        self.id = id
3 Likes