[swift-evolution] Keyword for protocol conformance

Charles Srstka cocoadev at charlessoft.com
Mon Aug 22 19:02:42 CDT 2016


On Aug 22, 2016, at 6:33 PM, Xiaodi Wu <xiaodi.wu at gmail.com> wrote:
> 
> That's a good point. Since you're proposing an optional keyword, though, aren't you describing a linter functionality?

If the optionality of the keyword bothers you, there are multiple ways to do it:

Solution #1: Optional keyword.

protocol P {
	func giveUpTheFunc()
}

struct S: P {
	implement func giveUpTheFunc()
}

Solution #2: Required keyword, but with a manual override for protocols that need to be retroactive.

protocol P {
	@retro func giveUpTheFunc()
}

struct S {
	func giveUpTheFunc()
}

extension S: P {}

Solution #3: No keyword, but extensions that conform to protocols can’t have anything other than protocol conformances unless they’re private.

protocol P {
	func giveUpTheFunc()
}

struct S {}

extension S: P {
	func giveUpTheFunc() {}
	func tearTheRoofOffTheSucker() {} // error!
}

but this is okay:

extension S: P {
	func giveUpTheFunc() {
		tearTheRoofOffTheSucker()
	}

	private func tearTheRoofOffTheSucker() {} // works :-)
}

There are definitely a range of ways to make protocol conformance more explicit. Which one is the best, I’m not sure, but we could probably flesh out the pros and cons of these, as well as any other options that I haven’t thought of, with some discussion.

Charles



More information about the swift-evolution mailing list