<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body><div>That kind of lazy checking of arrays is used pretty rarely (since, as you say, it only occurs with an `as!` expression). But doing lazy checking of strings would end up having to check&nbsp;<i>every</i> string that comes from ObjC (which, in a Swift app that uses Cocoa frameworks, is likely to be most strings the app works with).<br></div>
<div>&nbsp;</div>
<div>-Kevin Ballard</div>
<div>&nbsp;</div>
<div>On Mon, Jan 4, 2016, at 02:41 PM, Félix Cloutier wrote:<br></div>
<blockquote type="cite"><div>There are precedents for lazily checking for validity after bridging. Using `array as! [T]` on a NSArray without generics &nbsp;fails lazily if you access an object that's not a T.<br></div>
<div>&nbsp;</div>
<div><span class="colour" style="color:rgb(0, 0, 0)"><span class="font" style="font-family:'Lucida Grande'">Félix</span></span><br></div>
<div>&nbsp;</div>
<div><blockquote type="cite"><div>Le 4 janv. 2016 à 14:59:47, Dmitri Gribenko via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; a écrit :<br></div>
<div>&nbsp;</div>
<div><div dir="ltr"><div><div><div>On Mon, Jan 4, 2016 at 9:37 PM, Kevin Ballard via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt;</span> wrote:<br></div>
<blockquote style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0.8ex;border-left-width:1px;border-left-color:rgb(204, 204, 204);border-left-style:solid;padding-left:1ex;"><div><u></u><br></div>
<div><div>I agree in principle that it would be great if String could enforce that it's always valid.<br></div>
<div>&nbsp;</div>
<div>But unfortunately, in practice, there's no way to do that without making it expensive to bridge from Obj-C. Because, as you've demonstrated, you can create NSStrings that contain things that aren't actually valid unicode sequences, every single bridge from an NSString to a String would have to be checked for validity. Not only that, but it's not clear what the behavior would be if an invalid string is found, since these bridges are unconditional - would Swift panic? Would it silently replace the invalid sequence with U+FFFD? Or something else entirely? But the question doesn't really matter, because turning these bridges from O(1) into O(N) would be an unacceptable performance penalty anyway.<br></div>
</div>
</blockquote><div>&nbsp;</div>
<div>Currently String replaces invalid sequences with U+FFFD lazily during access, but there are corner cases related to Objective-C bridging that can still leak invalid Unicode.<br></div>
<div>&nbsp;</div>
<div>Dmitri<br></div>
</div>
<div>&nbsp;</div>
<div>-- <br></div>
<div><div>main(i,j){for(i=2;;i++){for(j=2;j&lt;i;j++){if(!(i%j)){j=0;break;}}if<br></div>
<div>(j){printf("%d\n",i);}}} /*Dmitri Gribenko &lt;<a href="mailto:gribozavr@gmail.com">gribozavr@gmail.com</a>&gt;*/<br></div>
</div>
</div>
</div>
<div><img style="height:1px !important;width:1px !important;border-top-width:0px !important;border-right-width:0px !important;border-bottom-width:0px !important;border-left-width:0px !important;margin-top:0px !important;margin-bottom:0px !important;margin-right:0px !important;margin-left:0px !important;padding-top:0px !important;padding-bottom:0px !important;padding-right:0px !important;padding-left:0px !important;" border="0" height="1" width="1" alt="" src="https://www.fastmailusercontent.com/proxy/c333863cc5ccd02f8cd270f7e2edad541a991fa1d48757eaae6bcb2525ec958f/8647470737a3f2f25723030323431303e23647e23756e64676279646e2e65647f27766f2f60756e6f35707e6d396259433265684455633558795148445c66533c6143383a794056684d486975725a77645d674b46563b67365034427a5b635334434a5a535d22364a7a6d22364943546549413978507874364c4567305465413838756467694a5560724a7649303c43405267477f62397434507d4b4a777d6c42763c6c427f6365397745657a4940595261696d22364b647336594d6d613a6d22364c634e477471464056336e683c42775361314e403753425772317a4f6436417872314267647663585e4c69694946577272766653596340335d6831774a657659546a4074647078476b6e4e43656165323878345371757973526a4c69353b6d23344/open">
_______________________________________________<br></div>
<div>swift-evolution mailing list<br></div>
<div><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br></div>
<div>https://lists.swift.org/mailman/listinfo/swift-evolution<br></div>
</div>
</blockquote></div>
</blockquote><div>&nbsp;</div>
</body>
</html>