<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 31, 2015, at 5:44 AM, Tino Heth &lt;<a href="mailto:2th@gmx.de" class="">2th@gmx.de</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">This is some sort of a cross-post from another thread ["automatic protocol forwarding"] — for anyone who wants to follow, I recommend to read&nbsp;<a href="https://kotlinlang.org/docs/reference/classes.html" class="">https://kotlinlang.org/docs/reference/classes.html</a><div class="">The idea of "function-like class declaration" has been introduced here by Joe Groff, but apparently, its benefits have been underestimated.</div><div class=""><br class=""><blockquote type="cite" class="">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.</blockquote><br class=""><div class=""></div><blockquote type="cite" class=""><div class="">Here is Kotlin:</div><div class="">class Person constructor(firstName: String, lastName: String) {</div><div class="">}</div></blockquote><div class="">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.</div></div></div></div></blockquote><div><br class=""></div><div>I was not trying to make Kotlin look worse. &nbsp;I copied from the Kotlin web page. &nbsp;I do see that constructor can be omitted when no modifiers are necessary. &nbsp;</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""><blockquote type="cite" class=""><div class="">Here is Swift under my proposal:</div><div class="">class Person {</div><div class="">&nbsp; &nbsp; var firstName: String</div><div class="">&nbsp; &nbsp; var lastName: String</div><div class="">&nbsp; &nbsp; // compiler synthesizes memberwise init</div><div class="">}</div><div class=""><br class=""></div><div class="">However, my proposal gives you a lot of additional flexibility:</div></blockquote><div class="">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…</div></div></div></div></blockquote><div><br class=""></div><div>Personally, I think it is a lot more readable to put members separate lines, but if you don’t like doing that:</div><div><br class=""></div><div>struct&nbsp;Person {&nbsp;var&nbsp;firstName:&nbsp;String;&nbsp;var&nbsp;lastName:&nbsp;String<br class="">&nbsp; &nbsp;&nbsp;// compiler synthesizes memberwise init<br class="">}</div><div><br class=""></div><div>There are no additional lines of code when you do it this way. &nbsp;You need to introduce the member declarations with `let` or `var` but that is not necessarily a bad thing.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""><blockquote type="cite" class=""><div class="">1. It interacts well with access control</div></blockquote><div class="">Better than Kotlin? Please prove this.</div></div></div></div></blockquote><div><br class=""></div><div>Please look at the examples I have in the Access Control section of the proposal. &nbsp;I spent some time reading the Kotlin docs and it isn’t clear to me that Kotlin can do this. &nbsp;But maybe it can. &nbsp;I don’t know Kotlin well. &nbsp;It sounds like you do, so if it can, please show how this is done in Kotlin.</div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""><blockquote type="cite" class=""><div class="">2. Partial memberwise initialization is possible</div></blockquote><div class="">The same with Kotlin — and imho at least as easy</div></div></div></div></blockquote><div><br class=""></div><div>That isn’t clear from the Kotlin docs. &nbsp;It may well be that they are just missing examples. &nbsp;Please post some samples showing how this is handled.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""><blockquote type="cite" class=""><div class="">3. It allows memberwise initializers to accept non-memberwise parameters to initialize private state</div></blockquote><div class="">I think this can be achieved with less effort using function-like class declaration (afair Joe already gave an example)</div></div></div></div></blockquote><div><br class=""></div>I don’t see either of the examples Joe posted doing this. &nbsp;Here is an example showing what I mean:</div><div><br class=""></div><div><pre style="box-sizing: border-box; overflow: auto; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; padding: 16px; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-wrap: normal; word-break: normal;" class=""><span style="color: rgb(51, 51, 51);" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">struct</span> S {
    <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> s: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span>
    <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">private</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> i: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Int</span>

    <span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// user declares:</span>
    memberwise <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">init</span>(other: S, <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">...</span>) {
        i <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">other.i</span>
    }
    <span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// compiler synthesizes (suppressing memberwise initialization for properties with lower visibility):</span>
    <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">init</span>(other:&nbsp;S, s: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span>) {
        <span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">/* synthesized */</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">self</span><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>s <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> s

        <span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// body of the user's initializer remains</span>
        i <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> </span><font color="#0086b3" class="">other.i</font><font color="#333333" class="">
    }
}</font></pre><div class=""><br class=""></div></div><div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""><blockquote type="cite" class=""><div class="">4. More than one memberwise initializer is possible</div></blockquote><div class="">Kotlin has no need for memberwise initializers at all, and I see this as a big advantage</div></div></div></div></blockquote><div><br class=""></div><div>Please explain how it is an advantage. &nbsp;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)?</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""><blockquote type="cite" class=""><div class="">5. Memberwise initialization of properties with declaration modifiers, behaviors / delegates is possible</div></blockquote><div class=""><a href="https://kotlinlang.org/docs/reference/delegated-properties.html" class="">https://kotlinlang.org/docs/reference/delegated-properties.html</a></div><div class="">(afaik this is not only possible, it's handled by the current compiler for a long time)</div></div></div></div></blockquote><div><br class=""></div><div>Yes, I know Kotlin has this feature. &nbsp;It isn’t clear from the docs how initialization of such properties is handled (for example an Observable property). &nbsp;Maybe you can provide some examples of how this works.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><div class=""><br class=""></div><blockquote type="cite" class=""><div class="">And probably more. &nbsp;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.</div></blockquote><div class=""><div class="">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.</div></div></div></div></div></blockquote><div><br class=""></div><div>This is currently a problem in Swift. &nbsp;I am confident that it can be solved one way or another. &nbsp;I don’t think a solution should be tied to the “type initializer parameter list” syntax.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><div class=""><br class=""></div><div class="">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:</div><div class="">It's more concise, it doesn't need "required" nor "convenience", it offers an intuitive syntax for forwarding and it is easy to grasp.</div></div></div></div></blockquote><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><div class=""><br class=""></div><div class="">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).</div></div></div></div></blockquote><div><br class=""></div><div>If you can clearly demonstrate how Kotlin is superior in a specific area I will give that great consideration. &nbsp;I want this proposal to be the best it can be. &nbsp;However, you’re going to need to do more than just link to the docs which I have already looked at.</div><div><br class=""></div><div>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. &nbsp;Think of it this way - this proposal provides a flexible and orthogonal foundation for memberwise initialization. &nbsp;If desired, a future enhancement could easily be developed to provide additional syntactic sugar on top of it. &nbsp;The example Joe posted shows how that might work. &nbsp;</div><div><br class=""></div><div>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. &nbsp;The new syntax should be evaluated independently as its own proposal. &nbsp;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).</div><div><br class=""></div><div>Matthew</div></div></body></html>