[swift-evolution] [Proposal Draft] Flexible memberwise initialization

Matthew Johnson matthew at anandabits.com
Thu Dec 31 09:17:23 CST 2015

> On Dec 31, 2015, at 5:44 AM, Tino Heth <2th at gmx.de> wrote:
> This is some sort of a cross-post from another thread ["automatic protocol forwarding"] — for anyone who wants to follow, I recommend to read https://kotlinlang.org/docs/reference/classes.html <https://kotlinlang.org/docs/reference/classes.html>
> The idea of "function-like class declaration" has been introduced here by Joe Groff, but apparently, its benefits have been underestimated.
>> If you feel Kotlin’s approach is better please respond to the memberwise initialization thread with some examples written in both Kotlin and in Swift using the memberwise initialization feature I am proposing to demonstrate how and why you think it is better.
>> Here is Kotlin:
>> class Person constructor(firstName: String, lastName: String) {
>> }
> That is a bad start — do you want to make Kotlin look worse than it is? You can just write "class Person(firstName: String, lastName: String)", the other syntax merely exists to allow designated constructors with special access restrictions.

I was not trying to make Kotlin look worse.  I copied from the Kotlin web page.  I do see that constructor can be omitted when no modifiers are necessary.  

>> Here is Swift under my proposal:
>> class Person {
>>     var firstName: String
>>     var lastName: String
>>     // compiler synthesizes memberwise init
>> }
>> However, my proposal gives you a lot of additional flexibility:
> I deny that, and even if it is true, there is a price to pay — and that is more than the lines of code that are required…

Personally, I think it is a lot more readable to put members separate lines, but if you don’t like doing that:

struct Person { var firstName: String; var lastName: String
    // compiler synthesizes memberwise init

There are no additional lines of code when you do it this way.  You need to introduce the member declarations with `let` or `var` but that is not necessarily a bad thing.

>> 1. It interacts well with access control
> Better than Kotlin? Please prove this.

Please look at the examples I have in the Access Control section of the proposal.  I spent some time reading the Kotlin docs and it isn’t clear to me that Kotlin can do this.  But maybe it can.  I don’t know Kotlin well.  It sounds like you do, so if it can, please show how this is done in Kotlin.

>> 2. Partial memberwise initialization is possible
> The same with Kotlin — and imho at least as easy

That isn’t clear from the Kotlin docs.  It may well be that they are just missing examples.  Please post some samples showing how this is handled.

>> 3. It allows memberwise initializers to accept non-memberwise parameters to initialize private state
> I think this can be achieved with less effort using function-like class declaration (afair Joe already gave an example)

I don’t see either of the examples Joe posted doing this.  Here is an example showing what I mean:

struct S {
    let s: String
    private let i: Int

    // user declares:
    memberwise init(other: S, ...) {
        i = other.i
    // compiler synthesizes (suppressing memberwise initialization for properties with lower visibility):
    init(other: S, s: String) {
        /* synthesized */ self.s = s

        // body of the user's initializer remains
        i = other.i

>> 4. More than one memberwise initializer is possible
> Kotlin has no need for memberwise initializers at all, and I see this as a big advantage

Please explain how it is an advantage.  How does Kotlin handle a case where you have some private state that needs to be initialized internally to protect invariants, but also some members which users can initialize (such as appearance attributes on a UI widget)?

>> 5. Memberwise initialization of properties with declaration modifiers, behaviors / delegates is possible
> https://kotlinlang.org/docs/reference/delegated-properties.html <https://kotlinlang.org/docs/reference/delegated-properties.html>
> (afaik this is not only possible, it's handled by the current compiler for a long time)

Yes, I know Kotlin has this feature.  It isn’t clear from the docs how initialization of such properties is handled (for example an Observable property).  Maybe you can provide some examples of how this works.

>> And probably more.  My approach was to design a solution that fits into the current Swift language and is orthogonal to other language features as much as possible.
> Afaics there not much room left for the promised additional flexibility… and the problem with default values for constants just doesn't exist in Kotlin at all.

This is currently a problem in Swift.  I am confident that it can be solved one way or another.  I don’t think a solution should be tied to the “type initializer parameter list” syntax.

> I neither state your proposal is bad, nor that we should simply copy an existing solution, but even as a big fan of Swift, I have to admit that Kotlin (which I probably will never use in real projects) clearly performs better in this area:
> It's more concise, it doesn't need "required" nor "convenience", it offers an intuitive syntax for forwarding and it is easy to grasp.
> I know that I'm not just challenging your own, personal ideas here, and have no illusions about the outcome of this bold move — but it is my honest opinion this proposal might (in total) be better than the status quo, but is inferior to Kotlin in every aspect (whereas the current scheme has the edge of familiarity on its side).

If you can clearly demonstrate how Kotlin is superior in a specific area I will give that great consideration.  I want this proposal to be the best it can be.  However, you’re going to need to do more than just link to the docs which I have already looked at.

The good news is that as far as I can tell the things you like about what Kotlin is doing are not mutually exclusive with this proposal at all.  Think of it this way - this proposal provides a flexible and orthogonal foundation for memberwise initialization.  If desired, a future enhancement could easily be developed to provide additional syntactic sugar on top of it.  The example Joe posted shows how that might work.  

If this proposal is accepted and you want to pursue a proposal for that additional layer of syntactic sugar to get closer to Kotlin syntax I encourage you to do that.  The new syntax should be evaluated independently as its own proposal.  I would be happy to help show how your desired syntax could be transformed into existing syntax (including the memberwise initialization syntax if this proposal is accepted).

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151231/cd119603/attachment.html>

More information about the swift-evolution mailing list