<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><br><br>Sent from my iPhone</div><div><br>On Jan 13, 2016, at 5:05 PM, Howard Lovatt via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr">@Robert,<div><br></div><div>The behaviour you show for Java arrays is what I am proposing, but that isn't type unsafe. The type error is detected at runtime and is flagged with an ArrayStoreException. This is unlike C for example that would allow the store, at least with a cast, &nbsp;and would put into the array the address of the string. Therefore neither the proposal or Java is type unsafe, both are type safe and both detect type errors at runtime.</div></div></div></blockquote><div><br></div><div>In case you hadn't noticed, Swift provides a basic form of covariance for the collections in the standard library... And it does so without the need for runtime checking, because value semantics allow safe mutation and collection covariance.</div><div><br></div><div>&nbsp; - Doug</div><div><br></div><br><blockquote type="cite"><div><div dir="ltr"><div><br></div><div>The question is whether protecting against this is worthwhile, not whether it can be protected against or not (it can be). Arrays are a good example, Swift takes the approach (like most languages) of checking the array size at runtime. But languages with dependent typing check the array size at compile time, i.e. a dependently typed language would detect this error:</div><div><br></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">let</span> array = [<span style="color:rgb(39,42,216)">1</span>]</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(79,129,135)">array</span><span style="color:rgb(0,0,0)">[</span><span style="color:rgb(39,42,216)">0</span><span style="color:rgb(0,0,0)">] </span>// 1, OK</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(79,129,135)">array</span><span style="color:rgb(0,0,0)">[</span><span style="color:rgb(39,42,216)">1</span><span style="color:rgb(0,0,0)">] </span>// Error not detected by compiler but detected at runtime</p></div><div><br></div><div>because in such a language array would be typed as int array of size one, not int array of any size.</div><div><br></div><div>So the real question is in a language like Java, how many times do you get ArrayStoreException? In my own code I have never seen an array store exception! Why? Because contravariant writes are very rare. If you were to protect against contravariant writes you would be complicating the language for no practical gain, which I think is a bad trade off.</div><div><br></div><div>Hope this explains the reasoning,</div><div><br></div><div>&nbsp;-- Howard.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On 14 January 2016 at 09:21, Developer <span dir="ltr">&lt;<a href="mailto:devteam.codafi@gmail.com" target="_blank">devteam.codafi@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div>It does indeed make the type system unsound in some cases.&nbsp; You have chosen one particular variance direction because it is convenient. A classic counterexample is Java making Arrays covariant by default.&nbsp; So this works</div><div><br></div><div>Integer[] arr = [2, 4, 6, 8, 10];</div><div>Object[] orr = arr;</div><div>orr[0] = "crash bang";</div><div><br></div><div>And crashes at runtime.&nbsp; For that, I must give this part of the proposal a strong -1.&nbsp; Any amount of type safety I have to give up in the name of convenience is far too much.</div><div><br></div><div>I am, however, a fan of generic protocols.&nbsp; They seem like an orthogonal concept given the first part here though.<br><br><div>~Robert Widmann</div></div><div><br>2016/01/13 17:03、Howard Lovatt via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; のメッセージ:<br><br></div><blockquote type="cite"><div><div><div class="h5"><div dir="ltr">@Thorsten,<div><br></div><div>It doesn't make the type system unsound; the types are mostly checked at compile time but some are runtime checked, either way the types are checked and therefore the type system is sound. I have posted an example of array runtime type checking in a previous response.</div><div><br></div><div>You can annotate variance, but this generally adds a lot of clutter to the code (see some typical Java code that uses generics you will see stuff like ? extends Type everywhere). In other languages like Scala the annotation is less visually intrusive, because they use shorter syntax and because it is annotated at declaration site rather than use site, but it is still there.</div><div><br></div><div>I think Swift arrays are a good example of partially static and partially runtime checked. The content is statically typed but the size is runtime typed. Other languages do type both the content and the size (see Dependent Typing on Wiki), however at some considerable burden on the programmer.&nbsp;</div><div><br></div><div>Hope this explains the thought process,</div><div><br></div><div>&nbsp;-- Howard.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On 13 January 2016 at 16:50, Thorsten Seitz <span dir="ltr">&lt;<a href="mailto:tseitz42@icloud.com" target="_blank">tseitz42@icloud.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Strong -1 from me as well for making the type system unsound.<br>
<span><br>
&gt; Am 13.01.2016 um 02:47 schrieb Howard Lovatt via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;:<br>
&gt;<br>
&gt; Yes you can annotate for covariance, invariance, and contravariance, both Java and Scala, allow all three. The problem is that the code becomes splattered with variance annotations<br>
<br>
</span>Ceylon uses a different approach which is variance annotations at the definition site.<br>
This restricts the possible usage of the type parameters to appropriately variant positions.<br>
<br>
This migt be a better way to deal with variance.<br>
<span><font color="#888888"><br>
-Thorsten</font></span></blockquote></div><br><br clear="all"><div><br></div>-- <br><div>&nbsp; -- Howard.<br></div>
</div>
</div></div><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=NX5pLVYXK-2FsoGvk-2BsN3ORSjM6f2-2BhaPL1hRr1LvA764W188Y4hXkZci0axvp1rUqknkfjhEEe0vXHLEB6J03VkYcVNX7ET3HQQnfCPaYb2REvCV4R1bKYzVxnTDil2420QDCaP3r5Bg-2Bz4OSuzU7nCwFa4vgm6wLiFpk6HgCxgGOItTHcGyPzazkfFtcPeSska7Vbim8jn5Ckafmk-2B8pNkpmIKx9smOZcSu3OedbvZA-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><span class=""><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></span></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature">&nbsp; -- Howard.<br></div>
</div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=XbU4wxnU83GyR7TGhOdBtBOchMF7PYvaYPXWDKda2ffb2nsatN7OlyuO9aXDSHARgBN4VDmHvo10HXMXJMuqFodTKQqIobczK2NIdKGFLQjBnWB9PLm-2F6HrA8L55Z6CENFwsds-2FdUe67w8QHeeCEQ6vWhRJcPp-2B2eQaINYeZCEVA2QBn-2BLNBhiF1kJM09oJmVTDbme9zX6c-2B-2B-2BFZwinueaVAPKWBoqj03hSEAVAxq5U-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-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></body></html>