SE-0242: Synthesize default values for the memberwise initializer


(Alejandro Alonso) #101

Forum post here: User defined initializers always call member initializers


User defined initializers always call member initializers
(Slava Pestov) #102

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


(Daryle Walker) #103

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.


(Lance Parker) #104

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