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

Sune Foldager cyano at me.com
Wed Jan 13 14:10:58 CST 2016


> On 12 Jan 2016, at 10:45, Howard Lovatt via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Currently you generics are invariant whereas function arguments etc. are covariant. I am suggesting that if the way generics are implemented is changed then they can be made covariant and that this will add considerable utility to Swift generics.

What about contravariant scenarios? Examples from C#:

IEnumerable<out T> // covariant
since if S : T then we want IEnumerable<S> : IEnumerable<T>

because IEnumerable<T> only returns T’s.

but then

IComparable<in T> // contravariant
since if S : T we definitely want IComparable<T> : IComparable<S>

because IComparable<T> only takes T’s as input.

reversing either of the above would make the type system unsound.
Any variance be default can’t be sound, and I am strongly opposed to that.

-Sune

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160113/45941320/attachment.html>


More information about the swift-evolution mailing list