Shorthand init

In my opinion, having

  1. just a single line propertyName
  2. which is sugar for self.propertyName = propertyName
  3. and only works within an init statement (presumably)

Just feels too unseen-magic-y. I also feel like this syntax starts to become a lot less clear when you have to do some processing within the initializer. For example,

struct Person {
    let name: String
    let occupation: String?

    init(firstName: String, lastName: String, occupation: String?) {
        self.name = firstName + " " + lastName
        occupation
    }
}

The case above is already quite simple, but I feel like it demonstrates how unclear this could end up being in a larger example since there is no indication of assignment at all.

The problem that this seems to address is making member wise inits easier to create - like Charles_Constant has said, the only reason I see this being necessary is if you need to make the member wise initializer public, or if you have multiple initializers and want one of them to be the member wise one.

I think the better solution for that would be Explicit Memberwise Initializers - #3 by dan-zheng. A member wise init under that suggestion would look like init(internal...).

Alternatively, a @MemberwiseInit macro, which just autogenerates a member wise init, might work as well.

2 Likes