<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="">-1 - this would disallow e.g. to share UI code between iOS and macOS:<div class=""><br class=""></div><div class="">#if os(iOS)</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>typealias XUView = UIView</div><div class="">#else</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>typealias XUView = NSView</div><div class="">#endif</div><div class=""><br class=""></div><div class="">extension XUView {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>...</div><div class="">}</div><div class=""><br class=""></div><div class="">or with any similar compatibility typealiases.</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jun 9, 2017, at 5:38 PM, Jacob Williams via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> 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="">+1 from me.<div class=""><br class=""></div><div class="">There have been times I’ve wanted to subclass an object (such as String) but since it is a non-class, non-protocol type you can only extend Strings existing functionality which adds that same functionality to Strings everywhere. It would be nice if we could either extend type aliases (and only the type alias), or if it were possible to inherit from structs so that we could create a custom string type like so:</div><div class=""><br class=""></div><div class=""><font face="Menlo" class="">struct HeaderKey: String {</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>static var lastModified: String { return “Last-Modified” }</font></div><div class=""><font face="Menlo" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>static var host: String { return “Host” }</font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><br class=""></div><div class="">I realize that struct inheritance is far less likely, since that defeats one of the main pieces of what makes a struct a struct. So I’m all for this proposal of allowing type aliases to be extended as though they were their own struct/class.</div><div class=""><br class=""></div><div class="">Unfortunately, I’m not sure how feasible this kind of functionality would actually be, but if it’s possible then I’m in favor of implementing it.</div><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Jun 8, 2017, at 10:14 PM, Yvo van Beek via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Typealiases can greatly reduce the complexity of code. But I think one change in how the compiler handles them could make them even more powerful.<div class=""><br class=""></div><div class="">Let's say I'm creating a web server framework and I've created a simple dictionary to store HTTP headers (I know that headers are more complex than that, but as an example). I could write something like this:</div><div class=""><br class=""></div><div class=""><div class=""> <font face="monospace, monospace" color="#0b5394" class="">typealias HeaderKey = String</font></div><div class=""><font face="monospace, monospace" color="#0b5394" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" color="#0b5394" class=""> var headers = [HeaderKey: String]()</font></div><div class=""><font face="monospace, monospace" color="#0b5394" class=""> headers["Host"] = "<a href="http://domain.com/" class="">domain.com</a>"</font></div></div><div class=""><br class=""></div><div class="">Now I can define a couple of default headers like this:</div><div class=""><br class=""></div><div class=""><div class=""><font color="#0b5394" face="monospace, monospace" class=""> extension HeaderKey {</font></div><div class=""><font color="#0b5394" face="monospace, monospace" class=""> static var lastModified: String { return "Last-Modified" }</font></div><div class=""><font color="#0b5394" face="monospace, monospace" class=""> static var host: String { return "Host" }</font></div><div class=""><font color="#0b5394" face="monospace, monospace" class=""> }</font></div></div><div class=""><br class=""></div><div class="">After that I can do this:<br class=""></div><div class=""><br class=""></div><div class=""><div class=""><span style="color:rgb(11,83,148);font-family:monospace,monospace" class=""> </span><font color="#0b5394" class=""><font face="monospace, monospace" class="">var headers = [HeaderKey: String]()</font></font></div><div class=""><span style="color:rgb(11,83,148);font-family:monospace,monospace" class=""> </span><font face="monospace, monospace" color="#0b5394" class="">headers[.host] = "<a href="http://domain.com/" class="">domain.com</a>"</font></div><div class=""><span style="color:rgb(11,83,148);font-family:monospace,monospace" class=""> </span><font face="monospace, monospace" color="#0b5394" class="">headers[.lastModified] = "some date"</font><font face="monospace, monospace" color="#0b5394" class=""><br class=""></font></div><div class=""><span style="color:rgb(11,83,148);font-family:monospace,monospace" class=""> </span><font face="monospace, monospace" color="#0b5394" class="">headers["X-MyHeader"] = "This still works too"</font></div></div><div class=""><font face="monospace, monospace" color="#0b5394" class=""><br class=""></font></div><div class="">But unfortunately the extension is also applied to normal strings:<font face="monospace, monospace" color="#0b5394" class=""><br class=""></font></div><div class=""><br class=""></div><div class=""> <font color="#0b5394" face="monospace, monospace" class="">var normalString: String = .host</font><br class=""></div><div class=""><font color="#0b5394" face="monospace, monospace" class=""><br class=""></font></div><div class="">Perhaps it would be better if the extension would only apply to the parts of my code where I use the HeaderKey typealias and not to all Strings. This could be a great tool to specialize classes by creating a typealias and adding functionality to it. Another example I can think of is typealiases for dictionaries or arrays with added business logic through extensions (especially since you can't inherit from structs).<font color="#0b5394" face="monospace, monospace" class=""><br class=""></font></div><div class=""><br class=""></div><div class="">If you want to create an extension that adds functionality to all Strings you could have created an extension for String instead of HeaderKey.</div><div class=""><br class=""></div><div class="">Please let me know what you think. I'm not sure how complex this change would be.</div><div class="">I could write a proposal if you're interested.</div><div class=""><br class=""></div><div class="">Kind regards,</div><div class="">Yvo</div></div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div></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>