<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>To bring a little bit more context: I copied this Regex library in my project which had StringLiteralConvertible and implemented the pattern matching operator and all of a sudden, ALL init(rawValue: String) calls of completely unrelated enums started returning unexpected values. If I did not have unit tests, I probably would not have found out about it for a while.</div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature">I understand the mechanism which calls StringLiteralConvertible's init(stringLiteral) under the hood:</div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature">let a: SomeStringLiteralConvertibleType = "hello"</div><div><br></div><div>I also understand the magic in the pattern matching operator. But I don't understand why when associating them together:</div><div><br></div><div>func ~=(lhs: <span style="background-color: rgba(255, 255, 255, 0);">SomeStringLiteralConvertibleType, rhs: String) -> Bool {</span></div><div><span style="background-color: rgba(255, 255, 255, 0);"> return false</span></div><div>}</div><div><br></div><div>Then creates his behaviour in all Enums with String raw values:</div><div><br></div><div>enum MyEnum: String {</div><div> case Super = "super"</div><div>}</div><div><br></div><div>let a = MyEnum(rawValue: "super") // nil</div><div><br></div><div>I can't figure out if this is just a confusing behaviour of Swift, in which case I want to write a proposal to make it less confusing, or if it is a big with Swift, in which case I should open a bug report.</div><div><br></div><div>David</div><div><br>On 05 Jan 2016, at 18:26, David Hart via swift-users <<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>> wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div><span style="background-color: rgba(255, 255, 255, 0);">How is it that Swift allows code like this:</span><div class=""><span style="background-color: rgba(255, 255, 255, 0);"><br class=""></span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);"><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 class=""><span style="background-color: rgba(255, 255, 255, 0);"><br class=""></span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);">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);"><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);"><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);"><br class=""></span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);">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);">What can we do about it?</span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);"><br class=""></span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);">David.</span></div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=nE9rxSXA5G4kxsTVkgv43hXwizS3O2z60WweqomIrdjaYepUWOzNIv51wL2XM-2FDxdvrjiuVGPgedxto2v-2BbQdnDbltpA50VG2WAE0QFlFDEPgF2Mcn8TeWarxsGNVKNa2-2BPLN8F8ugolwjpaRmF8MURPvmCvIPg26UnwcO7g-2F1roz1shdQ1AxVT8ORreBcqVOAzIw2pusowpMh2ivzPq9XM0cGkK8Gs6ZLvq65nCtx0-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;">
</div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-users mailing list</span><br><span><a href="mailto:swift-users@swift.org">swift-users@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-users">https://lists.swift.org/mailman/listinfo/swift-users</a></span><br></div></blockquote></body></html>