[swift-evolution] Make generics covariant and add generics to protocols

Developer devteam.codafi at gmail.com
Wed Jan 13 16:21:00 CST 2016


It does indeed make the type system unsound in some cases.  You have chosen one particular variance direction because it is convenient. A classic counterexample is Java making Arrays covariant by default.  So this works

Integer[] arr = [2, 4, 6, 8, 10];
Object[] orr = arr;
orr[0] = "crash bang";

And crashes at runtime.  For that, I must give this part of the proposal a strong -1.  Any amount of type safety I have to give up in the name of convenience is far too much.

I am, however, a fan of generic protocols.  They seem like an orthogonal concept given the first part here though.

~Robert Widmann

2016/01/13 17:03、Howard Lovatt via swift-evolution <swift-evolution at swift.org> のメッセージ:

> @Thorsten,
> 
> 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.
> 
> 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.
> 
> 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. 
> 
> Hope this explains the thought process,
> 
>  -- Howard.
> 
>> On 13 January 2016 at 16:50, Thorsten Seitz <tseitz42 at icloud.com> wrote:
>> Strong -1 from me as well for making the type system unsound.
>> 
>> > Am 13.01.2016 um 02:47 schrieb Howard Lovatt via swift-evolution <swift-evolution at swift.org>:
>> >
>> > 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
>> 
>> Ceylon uses a different approach which is variance annotations at the definition site.
>> This restricts the possible usage of the type parameters to appropriately variant positions.
>> 
>> This migt be a better way to deal with variance.
>> 
>> -Thorsten
> 
> 
> 
> -- 
>   -- Howard.
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160113/5447ba4e/attachment.html>


More information about the swift-evolution mailing list