Why does the compiler not see the default code in a protocol?

半城伤御伤魂 提交于 2019-12-02 04:58:56

问题


Edit: I have restated and hopefully clarified this question over here. Now I've added the solution.

I've defined a function (see foo() in attached example) as a default function for structs adopting my protocol. It applies the + operator defined in respect of two other variables which themselves adopt other protocols and + is defined in one of those protocols. The variables are typed using associatedtypes. I get the message:

Binary operator '+' cannot be applied to operands of type 'Self.PointType' and 'Self.VectorType'

If I implement the function inside my struct (see bar() in attached) it works so I'm sure my + operator does work. My example is pared down to the minimum needed to work in a playground. Just remove the comments in the LineProtocol extension to get the error. It seems to me that Self.PointType is a Point and Self.VectorType is a Vector.

To be clear: The reason I used associatedtypes is because many different structs adopt each of the three protocols in the example so I can't name them directly

public protocol PointProtocol {
   associatedtype VectorType: VectorProtocol
   var elements: [Float] { get set }
}

extension PointProtocol {
   public static func +(lhs: Self, rhs:VectorType) -> Self {
      var translate = lhs
      for i in 0..<2 { translate.elements[i] += rhs.elements[i] }
      return translate
   }
}

public protocol VectorProtocol {
   associatedtype VectorType: VectorProtocol
   var elements: [Float] { get set }
}

public struct Point: PointProtocol {
   public typealias PointType = Point
   public typealias VectorType = Vector
   public var elements = [Float](repeating: 0.0, count: 2)

   public init(_ x: Float,_ y: Float) {
      self.elements = [x,y]
   }
}

public struct Vector: VectorProtocol {
   public typealias VectorType = Vector
   public static let dimension: Int = 2
   public var elements = [Float](repeating:Float(0.0), count: 2)

   public init(_ x: Float,_ y: Float) {
      self.elements = [x,y]
   }
}

public protocol LineProtocol {
   associatedtype PointType: PointProtocol
   associatedtype VectorType: VectorProtocol
   var anchor: PointType { get set }
   var direction: VectorType { get set }
}

extension LineProtocol {
//   public func foo() -> PointType {
//      return (anchor + direction)
//   }
}

public struct Line: LineProtocol {
   public typealias PointType = Point
   public typealias VectorType = Vector
   public var anchor: PointType
   public var direction: VectorType

   public init(anchor: Point, direction: Vector) {
      self.anchor = anchor
      self.direction = direction
   }

   public func bar() -> Point {
      return (anchor + direction)
   }
}

let line = Line(anchor: Point(3, 4), direction: Vector(5, 1))
print(line.bar())
//print(line.foo())

Solution adapted from @Honey's suggestion: replace extension with:

extension LineProtocol where Self.VectorType == Self.PointType.VectorType {
   public func foo() -> PointType {
      // Constraint passes VectorType thru to the PointProtocol
      return (anchor + direction)
   }
}


回答1:


I know what the problem is. Not sure if my solution is the best answer.

The problem is that both your associatedtypes have associatedtypes themselves.

So in the extension, the Swift compiler can't figure out the type of the associatedtypes — unless you constrain it.

Like do:

extension LineProtocol where Self.VectorType == Vector, Self.PointType == Point {
    public func foo() -> Self.PointType {
      return (anchor + direction)
   }
}

Your code works for your concrete type Line, because both your associatedtypes have their requirements fulfilled ie:

public typealias PointType = Point // makes compiler happy!
public typealias VectorType = Vector  // makes compiler happy!

FWIW you could have got rid of the explicit conformance to your associatedtype requirements and let the compiler infer1 conformance to your associatedtypes requirements and write your Line type as such:

public struct Line: LineProtocol {

   public var anchor: Point
   public var direction: Vector

   public init(anchor: Point, direction: Vector) {
      self.anchor = anchor
      self.direction = direction
   }

   public func bar() -> Point {
      return (anchor + direction)
   }
}

1: Generics - Associated Types

Thanks to Swift’s type inference, you don’t actually need to declare a concrete Item of Int as part of the definition of IntStack. Because IntStack conforms to all of the requirements of the Container protocol, Swift can infer the appropriate Item to use, simply by looking at the type of the append(_:) method’s item parameter and the return type of the subscript. Indeed, if you delete the typealias Item = Int line from the code above, everything still works, because it’s clear what type should be used for Item.



来源:https://stackoverflow.com/questions/58712685/why-does-the-compiler-not-see-the-default-code-in-a-protocol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!