[swift-users] A somewhat stupid question about protocol conformances

Antonino Ficarra antonino.ficarra at gmail.com
Sat Nov 25 00:40:17 CST 2017


In this code example:

import Foundation

protocol AreaProtocol {
	func	area() -> CGFloat
	
	// implemented in protocol extension
	func	volume( height:CGFloat ) -> CGFloat
	// ....
}

extension AreaProtocol {
	func	volume( height:CGFloat ) -> CGFloat {
		return height * area()
	}
	// ....
	// ....
}


// conform CGPoint sequences to AreaProtocol
extension Sequence
	where Self : AreaProtocol, Element == CGPoint
{
	func area() -> CGFloat {
		return 0.0	// ... poligon area
	}
}

let p0 = CGPoint(x: 0.0, y: 0.0)
let p1 = CGPoint(x: 2.0, y: 0.0)
let p2 = CGPoint(x: 2.0, y: 2.0)
let p3 = CGPoint(x: 0.0, y: 2.0)

let poligon = [p0,p1,p2,p3]
let a  = poligon.area()			// ! Type '[CGPoint]' does not conform to protocol 'AreaProtocol'
let v  = poligon.volume( height:10.0 )	// ! Value of type '[CGPoint]' has no member 'volume'


An array of CGPoint is a CGPoint sequence? Why the array don't gets automatic conformance to AreaProtocol?
How can conform an array of CGPoint to AreaProtocol?

Sorry for my bad english,
Antonino

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20171125/2511bfe5/attachment.html>


More information about the swift-users mailing list