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

Tony Parker anthony.parker at apple.com
Fri Feb 12 13:58:05 CST 2016


> On Feb 12, 2016, at 1:28 AM, Marco Masser via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 
>> 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!

Close… but every Objective-C method that takes an NSCountedSet assumes that that NSCountedSet is a subclass of NSObject.

There are some other complexities to this approach as well: how do we deal with types that are pointers-to-pointers in Objective-C? (API that takes NSCountedSet **, e.g.). What about types where frameworks layer on functionality via categories?

I have some ideas on this topic but I’m still working on it.

- Tony

> 
> • 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 😀
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list