<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="">How do you deal with the fact that structs have value semantic, while your generated class have reference semantic ?<div class=""><br class=""><div><blockquote type="cite" class=""><div class="">Le 4 déc. 2015 à 23:06, Dan Stenmark <<a href="mailto:daniel.j.stenmark@gmail.com" class="">daniel.j.stenmark@gmail.com</a>> a écrit :</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=""><div style="margin: 0px; line-height: normal;" class="">Swift’s Struct type is one of the most praised features of the language, but is currently unavailable in Objective-C. This poses problems for large legacy codebases that can’t be ported to Swift as quickly but still want to begin using some of the mutability semantics it introduces. As such, I’d like to propose syntax for creating bridged classes that can utilized in Objective-C.</div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><font face="Courier New" class="">@objc struct Letter {</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Courier New" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>var address : String?</font></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><font face="Courier New" class=""><br class=""></font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Courier New" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>init(withAddress address : String? ) {</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Courier New" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>self.address = address</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Courier New" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Courier New" class="">}</font></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">At compile-time, this would create two bridge classes: <b class="">Letter</b> and <b class="">MutableLetter</b>, both conforming to the NSMutableCopying protocol.</div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><font face="Courier New" class="">Letter *letter = [[Letter alloc] initWithAddress:address]; // equivalent to ‘let letter = Letter(address: address)’</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Courier New" class="">MutableLetter *mutableLetter = letter.mutableCopy; // equivalent to ‘var mutableLetter = letter’</font></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">With Objective-C’s lack of namespacing, the @objc decorator would likely need to accept an optional class prefix parameter to help guard against class name collisions.</div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><font face="Courier New" class="">@objc(US) struct Letter {</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Courier New" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>var address : String?</font></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><font face="Courier New" class=""><br class=""></font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Courier New" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>init(withAddress address : String? ) {</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Courier New" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>self.address = address</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Courier New" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Courier New" class="">}</font></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">-------</div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><font face="Courier New" class="">USLetter *letter = [[USLetter alloc] initWithAddress:address]; // equivalent to ‘let letter = Letter(address: address)’</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Courier New" class="">USMutableLetter *mutableLetter = letter.mutableCopy; // equivalent to ‘var mutableLetter = letter’</font></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">Nested types would also be represented via this proposal:</div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><font face="Courier New" class="">@objc(US) class PostalService {</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Courier New" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>struct Letter {</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Courier New" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>var address : String?</font></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><font face="Courier New" class=""><br class=""></font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Courier New" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>init(withAddress address : String? ) {</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Courier New" class=""><span class="Apple-tab-span" style="white-space:pre">                        </span>self.address = address</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Courier New" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>}</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Courier New" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Courier New" class="">}</font></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; line-height: normal;" class="">-------</div><div class=""><br class=""></div></div><div style="margin: 0px; line-height: normal;" class=""><font face="Courier New" class="">USPostalServiceLetter *letter = [[USPostalServiceLetter alloc] initWithAddress:address]; // equivalent to ‘let letter = PostalService.Letter(address: address)’</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Courier New" class="">USMutablePostalServiceLetter *mutableLetter = letter.mutableCopy; // equivalent to ‘var mutableLetter = letter’</font></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">Dan</div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=UqrBeBSFFZ3qCW2UhjXSFoWIxUt0IgTDHv8EhLubaACxArhJoHYj6upL4rMU69Mlyi6X90dl6nyLQTniFm0bULtjybKr8lzkfUMsVUpGDqSsvSEs3epH1VcVBYZv1z0zhhqpq2WPZCtGwGYejaIqi8yS3Dti7qYwyqoHNfCASZ5GQ9UU8tazV-2FIwZ5hNq1ysVl0N7dpPnd7ULPds1AgcWw7Vffrk4Q3C5klMWzqX5Pc-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;" class="">
</div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div></body></html>