<div dir="ltr">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><br></div><div>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><br></div><div><div> <font face="monospace, monospace" color="#0b5394">typealias HeaderKey = String</font></div><div><font face="monospace, monospace" color="#0b5394"><br></font></div><div><font face="monospace, monospace" color="#0b5394"> var headers = [HeaderKey: String]()</font></div><div><font face="monospace, monospace" color="#0b5394"> headers["Host"] = "<a href="http://domain.com">domain.com</a>"</font></div></div><div><br></div><div>Now I can define a couple of default headers like this:</div><div><br></div><div><div><font color="#0b5394" face="monospace, monospace"> extension HeaderKey {</font></div><div><font color="#0b5394" face="monospace, monospace"> static var lastModified: String { return "Last-Modified" }</font></div><div><font color="#0b5394" face="monospace, monospace"> static var host: String { return "Host" }</font></div><div><font color="#0b5394" face="monospace, monospace"> }</font></div></div><div><br></div><div>After that I can do this:<br></div><div><br></div><div><div><span style="color:rgb(11,83,148);font-family:monospace,monospace"> </span><font color="#0b5394"><font face="monospace, monospace">var headers = [HeaderKey: String]()</font></font></div><div><span style="color:rgb(11,83,148);font-family:monospace,monospace"> </span><font face="monospace, monospace" color="#0b5394">headers[.host] = "<a href="http://domain.com">domain.com</a>"</font></div><div><span style="color:rgb(11,83,148);font-family:monospace,monospace"> </span><font face="monospace, monospace" color="#0b5394">headers[.lastModified] = "some date"</font><font face="monospace, monospace" color="#0b5394"><br></font></div><div><span style="color:rgb(11,83,148);font-family:monospace,monospace"> </span><font face="monospace, monospace" color="#0b5394">headers["X-MyHeader"] = "This still works too"</font></div></div><div><font face="monospace, monospace" color="#0b5394"><br></font></div><div>But unfortunately the extension is also applied to normal strings:<font face="monospace, monospace" color="#0b5394"><br></font></div><div><br></div><div> <font color="#0b5394" face="monospace, monospace">var normalString: String = .host</font><br></div><div><font color="#0b5394" face="monospace, monospace"><br></font></div><div>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"><br></font></div><div><br></div><div>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><br></div><div>Please let me know what you think. I'm not sure how complex this change would be.</div><div>I could write a proposal if you're interested.</div><div><br></div><div>Kind regards,</div><div>Yvo</div></div>