<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 Jan 5, 2016, at 9:28 AM, David Hart via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@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 dir="auto" class=""><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">How is it that Swift allows code like this:</span><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><font class="">struct Sneaky: StringLiteralConvertible {<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>init(stringLiteral value: String) {}<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>init(extendedGraphemeClusterLiteral value: String) {}<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>init(unicodeScalarLiteral value: String) {}<br class="">}<br class=""><br class="">func ~=(sneaky: Sneaky, string: String) -> Bool {<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>return false<br class="">}<br class=""><br class="">enum NormalEnum: String {<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>case Super = "super"<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>case Mario = "mario"<br class="">}</font><br class=""><br class=""><font class="">let value = NormalEnum(rawValue: "super”) // return nil!!!!</font></span></div></div></div></div></blockquote><div><br class=""></div><div>I see two bugs here. When an enum has a raw value type, the compiler generates this initializer:</div><div><br class=""></div><div>init(rawValue: String) {</div><div> switch rawValue {</div><div> case "super":</div><div> self = .Super</div><div> ...</div><div> }</div><div>}</div><div><br class=""></div><div>so uses ~= pattern matching to match the raw value. It would be more sensible to always use `==` comparison in the synthesized initializer. However, I'm surprised too that the type checker favors ~=(Sneaky, String) over ~=(String, String); it should at best be ambiguous. Do you have time to file these two bugs?</div><div><br class=""></div><div>-Joe</div><br class=""><blockquote type="cite" class=""><div class=""><div dir="auto" class=""><div class=""><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">It hit completely by surprise today because of of a Regex library:</span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class=""><font class="" style="background-color: rgba(255, 255, 255, 0);">struct Regex: StringLiteralConvertible {<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>init(stringLiteral value: String) {}<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>init(extendedGraphemeClusterLiteral value: String) {}<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>init(unicodeScalarLiteral value: String) {}</font></div><div class=""><font class="" style="background-color: rgba(255, 255, 255, 0);"><br class=""></font></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><font class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>//...<br class="">}<br class=""><br class="">func ~=(regex: Regex, string: String) -> Bool {<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>return regex.matches(string)<br class="">}</font><br class=""></span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">If I was not already a Swift enthusiast, this behaviour would have left me completely dumbfounded.</span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">What can we do about it?</span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">David.</span></div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=RoDF4MveSEMYBIqIJA6ub1g8cOZ-2BVYvqV-2FqygPhjPn8la6RfEy4Xzrkx-2FRVcVtWkAT3LcwLZjjs31vaTGAaXl4SGGim3oKWvH5ZbXGF9iiibiRxeh-2BLE1HfGvnTdZ0cpNuVDB3OsnoMT2sGnZIPuM1a4o8cdCvPkypBGkSAualInrOnCtalj0tqnky2PGY261Q6eSl-2B-2FOJ-2FtR5BRF6-2BTSh-2FPb5lJTMRo15-2FCCSYJVrw-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-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></body></html>