[swift-evolution] [discussion] What generics feature would this be?

Austin Zheng austinzheng at gmail.com
Wed Aug 9 13:17:18 CDT 2017


A existential type of some protocol `P`, counterintuitively, doesn't conform to itself, unless it's an Objective-C protocol.

Because `S : Special` means S has to be a type that conforms to the protocol `Special`, and `special` is of the existential type `Special`, the existential type `Special` doesn't conform to the protocol `Special`, so it can't be used for `S`.

Best,
Austin

> On Aug 9, 2017, at 7:19 AM, Mathew Huusko V via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 
> Curious what part of generics the below would fall under? This hit me by surprise/makes even non-generic/Self-depending protocols un-interchangeable with classes..
> 
> ```
> protocol Special {}
> 
> func doWithAndReturn<S: Special>(_ special: S) -> S { ... }
> 
> let special: Special = ...
> 
> // "error: Generics parameter 'S' could not be inferred"
> // works fine when 'Special' is a class
> let newSpecial: Special = doWithAndReturn(special)
> ```
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list