[swift-evolution] protocol can only be used as a generic constraint because it has Self or associated type requirements

ilya ilya.nikokoshev at gmail.com
Mon Dec 14 01:40:08 CST 2015


You can achieve the same result more cleanly with

func maxY<T:P, U:P>(p1:T, p2: U) -> Int {
  return max(p1.y, p2.y)   // No problems here!
}

On Mon, Dec 14, 2015 at 10:03 AM, Paul Cantrell via swift-evolution <
swift-evolution at swift.org> wrote:

> On Dec 13, 2015, at 11:56 PM, Dave Abrahams via swift-evolution <
> swift-evolution at swift.org> wrote:
>
> On Dec 13, 2015, at 3:55 PM, Marc Knaup via swift-evolution <
> swift-evolution at swift.org> wrote:
>
> Is there any info yet if and how we will be able to refer to instances of
> protocols that have associated types?
>
>
> As far as I know, this isn't a solvable problem.
>
>
> What is the difficulty in supporting this?
>
>
> Here's a simple example:
>
> protocol P {
>   typealias A
>   var x: A { get set }
> }
>
> struct Y : P {
>   var x: Int
> }
>
> struct Z : P {
>   var x: String
> }
>
> func f(p1: P, p2: P) {
>   p1.x = p2.x // assigning an Int to a String?
> }
>
>
> p1.x = p2.x should be a compiler error, because there’s not enough type
> information. But that shouldn’t stop a programmer from doing this:
>
> protocol P {
>   typealias A
>   var x: A { get set }
>   var y: Int
> }
>
> struct Y : P {
>   var x: Int
>   var y: Int
> }
>
> struct Z : P {
>   var x: String
>   var y: Int
> }
>
> func maxY(p1: P, p2: P) -> Int {
>   return max(p1.y, p2.y)   // No problems here!
> }
>
> …right?
>
> Cheers, P
>
>
> _______________________________________________
> 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/20151214/deddc060/attachment.html>


More information about the swift-evolution mailing list