[swift-evolution] [Review] SE-0005 Better Translation of Objective-C APIs Into Swift

Marco Masser lists at duckcode.com
Fri Feb 12 03:28:17 CST 2016

> On 2016-02-11, at 06:32, Andrew Bennett <cacoyi at gmail.com> wrote:
> I like your idea, I think you may need a protocol for each class though. It's possible for NSNumber to be used as an NSValue, for example.

You’re right, that’s one case where the missing inheritance hierarchy after changing to structs would cause troubles. Using protocols to fix that is a good idea that has two implications I think are worth mentioning:

• On the plus side: Converting Objective-C classes into protocols along with a conforming struct would make something like inheritance possible again via protocol conformance.
For example, if the NSCountedSet Objective-C class became a CountedSet Swift protocol along with a CountedSetImpl (or whatever) Swift struct that conforms to it, you could write your own type that conforms to the CountedSet protocol and use that with every existing Objective-C code that expects an NSCountedSet. That sounds super awesome!

• On the minus side: As the above point shows, this indirection via protocols makes everything more complex.
Maybe this takes just some getting used to Protocol Oriented Programming, but I’m not sure that if I’d design an API right now that has to work on a new type Foo that I’d declare a protocol called Foo along with a FooImpl (or whatever) class or struct.
OK, maybe I would 😀

More information about the swift-evolution mailing list