[swift-evolution] [Pitch] Introducing role keywords to reduce hard-to-find bugs

Tino Heth 2th at gmx.de
Fri Jun 16 13:21:03 CDT 2017


The described problem might be one of the most famous itches of the language, but imho the bar for new keywords* should be higher than that — and there are alternatives:

First, I guess many would like to see this to be valid Swift:

protocol Foo {
	func bar() {
		print("Default implementation called")
	}
}

It's the most convenient way of avoiding typos: avoid to type ;-)
Imho this might already be enough, but for a full alternative for "default", I'd suggest something like this:

extension Foo {
	func Foo.bar() {
		print("String has its own implementation")
	}
}

(to make it more familiar for those with a C++ background, "Foo::bar" could be used instead ;-)

Additional benefit: This wouldn't be limited to protocols — and it could possibly help in weird situations when two protocols declare functions with identical signature...

extension String: Foo {
	func Foo.bar() {
		print("String has its own implementation")
	}

	func Foo.barr() {
		// compiler error, Foo defines no function "barr"
	}

	func barr() {
		// this is fine, no connection to a protocol
	}
}

- Tino

* well, I guess "default" is not really a new keyword… but you get the point
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170616/cd5492ec/attachment.html>


More information about the swift-evolution mailing list