<div dir="ltr">Hi,<div><br></div><div>Could you point to what Regex code are you using? You mention you copy some library but don&#39;t provide the original source.</div></div><br><div class="gmail_quote"><div dir="ltr">On Wed, Jan 6, 2016 at 9:12 AM David Hart via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div>I&#39;m starting to understand. The implementation of RawRepresentable&#39;s probably looks like this:</div><div><br></div><div>init?(rawValue: String) {</div><div>    switch rawValue {</div><div>    case &quot;firstValue&quot;: self = .FirstCase</div><div>    case &quot;secondValue&quot;: self = .SecondCase</div><div>    default: return nil</div><div>    }</div><div>}</div><div><br></div><div>In that case (no pun intended), the switch cases are converted to <span style="background-color:rgba(255,255,255,0)">SomeStringLiteralConvertibleType and then pattern matched.</span></div><div><span style="background-color:rgba(255,255,255,0)"><br></span></div><div><span style="background-color:rgba(255,255,255,0)">But shouldn&#39;t the implementation of switch refrain from any complicated casting when the types correspond?</span></div><div><span style="background-color:rgba(255,255,255,0)"><br></span></div><div><span style="background-color:rgba(255,255,255,0)">David.</span></div></div><div dir="auto"><div><br>On 06 Jan 2016, at 10:20, David Hart via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><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><br></div><div>I understand the mechanism which calls StringLiteralConvertible&#39;s init(stringLiteral) under the hood:</div><div><br></div><div>let a: SomeStringLiteralConvertibleType = &quot;hello&quot;</div><div><br></div><div>I also understand the magic in the pattern matching operator. But I don&#39;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) -&gt; 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 = &quot;super&quot;</div><div>}</div><div><br></div><div>let a = MyEnum(rawValue: &quot;super&quot;) // nil</div><div><br></div><div>I can&#39;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 &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><div><span style="background-color:rgba(255,255,255,0)">How is it that Swift allows code like this:</span><div><span style="background-color:rgba(255,255,255,0)"><br></span></div><div><span style="background-color:rgba(255,255,255,0)"><font>struct Sneaky: StringLiteralConvertible {<br><span style="white-space:pre-wrap">        </span>init(stringLiteral value: String) {}<br><span style="white-space:pre-wrap">        </span>init(extendedGraphemeClusterLiteral value: String) {}<br><span style="white-space:pre-wrap">        </span>init(unicodeScalarLiteral value: String) {}<br>}<br><br>func ~=(sneaky: Sneaky, string: String) -&gt; Bool {<br><span style="white-space:pre-wrap">        </span>return false<br>}<br><br>enum NormalEnum: String {<br><span style="white-space:pre-wrap">        </span>case Super = &quot;super&quot;<br><span style="white-space:pre-wrap">        </span>case Mario = &quot;mario&quot;<br>}</font><br><br><font>let value = NormalEnum(rawValue: &quot;super”) // return nil!!!!</font></span></div><div><span style="background-color:rgba(255,255,255,0)"><br></span></div><div><span style="background-color:rgba(255,255,255,0)">It hit completely by surprise today because of of a Regex library:</span></div><div><span style="background-color:rgba(255,255,255,0)"><br></span></div><div><font style="background-color:rgba(255,255,255,0)">struct Regex: StringLiteralConvertible {<br><span style="white-space:pre-wrap">        </span>init(stringLiteral value: String) {}<br><span style="white-space:pre-wrap">        </span>init(extendedGraphemeClusterLiteral value: String) {}<br><span style="white-space:pre-wrap">        </span>init(unicodeScalarLiteral value: String) {}</font></div><div><font style="background-color:rgba(255,255,255,0)"><br></font></div><div><span style="background-color:rgba(255,255,255,0)"><font><span style="white-space:pre-wrap">        </span>//...<br>}<br><br>func ~=(regex: Regex, string: String) -&gt; Bool {<br><span style="white-space:pre-wrap">        </span>return regex.matches(string)<br>}</font><br></span></div><div><span style="background-color:rgba(255,255,255,0)"><br></span></div><div><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><span style="background-color:rgba(255,255,255,0)">What can we do about it?</span></div><div><span style="background-color:rgba(255,255,255,0)"><br></span></div><div><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="min-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" target="_blank">swift-users@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a></span><br></div></blockquote>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=nE9rxSXA5G4kxsTVkgv43hXwizS3O2z60WweqomIrdjAKjW-2Fo8Z-2BhE0TERaKhx79UBgo55dgrU3vKcg1WikyLIXrmLr3mHEBvy-2FWLtPk5AYENyLb2Umzx91ogVckloDToE5z0kQw8jjFMQOp-2BzDdCUR4asM2ubcCNiL-2F9Ff8HR7ekIFDifSZI8cXgyYbFsp6KUEPXjHJvH3Ga-2FXtAowrED3etP-2BtvwQgYxTATKJkSY8-3D" alt="" width="1" height="1" border="0" style="min-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></div><div dir="auto"><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=iFFbT7PJPXnSbZ808eVurEghod2D5QOb3GbjXM6ibOc3FyjzkFe8r9reL6tcxs68zQxv17OwMR6lAP-2Faq7HEt6fT0NTXSZcZuJOxczSgKLf-2B2o-2Fav3jAWyEicR7lF8Wy2Ok-2FnewntoEqHo1O9d4Kd10-2FgSyCJN-2FP3CQyvaGprR8JB6eIXWYZw4VeQ0SsbpbQwPSVTPNNnBmdKYKIkAjR4uYyCrKQ4pD-2FjCLh6o-2Box-2FI-3D" alt="" width="1" height="1" border="0" style="min-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>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div>