[swift-evolution] Make generics covariant and add generics to protocols
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.
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.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the swift-evolution