[swift-users] Experimenting with conditional conformances
Antonino Ficarra
antonino.ficarra at gmail.com
Tue Nov 28 14:31:12 CST 2017
I’have a protocol:
protocol Inertial {
func convexHull( _ t:AffineTransform? ) -> [CGPoint]
func area( _ t:AffineTransform? ) -> CGFloat
func firstOrderMomentums( _ t:AffineTransform? ) -> (x:CGFloat,y:CGFloat)
func secondOrderMomentums( _ t:AffineTransform? ) -> (xx:CGFloat,xy:CGFloat,yy:CGFloat)
}
I make [CPoint] (a poligon) conforms to the protocol:
extension Array : Inertial where Element == CGPoint {
func convexHull(_ t: AffineTransform?) -> [CGPoint] {
return ConvexHull.convexHull( points:self ).map { $0 * t }
}
func area(_ t: AffineTransform?) -> CGFloat {
return InertialUti.area( polig: self,t )
}
func firstOrderMomentums(_ t: AffineTransform?) -> (x: CGFloat, y: CGFloat) {
return InertialUti.firstOrderMomentums( polig:self,t )
}
func secondOrderMomentums(_ t: AffineTransform?) -> (xx: CGFloat, xy: CGFloat, yy: CGFloat) {
return InertialUti.secondOrderMomentums( polig:self,t )
}
}
and work like a charm.
Then I try to make [[CPoint]] (an array of poligons) conforms to the same protocol:
extension Array : Inertial where Element == [CGPoint] {
var allVertexs : [CGPoint] {
return flatMap({$0})
}
func convexHull(_ t: AffineTransform?) -> [CGPoint] {
return ConvexHull.convexHull( points:allVertexs ).map { $0 * t }
}
func area(_ t: AffineTransform?) -> CGFloat {
return InertialUti.area( poligs: self,t )
}
func firstOrderMomentums(_ t: AffineTransform?) -> (x: CGFloat, y: CGFloat) {
return InertialUti.firstOrderMomentums( poligs:self,t )
}
func secondOrderMomentums(_ t: AffineTransform?) -> (xx: CGFloat, xy: CGFloat, yy: CGFloat) {
return InertialUti.secondOrderMomentums( poligs:self,t )
}
}
but this isn’t permitted: Redundant conformance of 'Array<Element>' to protocol ‘Inertial’ !
I think it's a severe limitation: array with different element’s type (i.e. different specializations of the same generic type) should be treated as distinct types and not as the same type.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20171128/483818f6/attachment.html>
More information about the swift-users
mailing list