4 ms·
> How exactly are protocols a Swift "upgrade" to OO programming? They were in Objective-C since the mid 90s, adopted by Java as interfaces, copied by C# etc. T
by dwaite 3y ago
> How exactly are protocols a Swift "upgrade" to OO programming? They were in Objective-C since the mid 90s, adopted by Java as interfaces, copied by C# etc.
Typically you would write more protocol-oriented code - rather than using inheritance (which is mostly there for Objective-C compatibility) you define protocols and implement them for types.
This is a lot closer to traits in Rust than interfaces in Java. Among other things, a developer can define how a third party type implements a protocol they control, without subclassing or wrapping, as long as it does not require additional data.
- pjmlp 3y agoSo just like lots of Objective-C code, including the old NeXTSTEP Kits?
- jb1991 3y agoSwift's protocols are an evolution of protocols in ObjC. They offer additional features to make them safer and easier to write.
- pjmlp 3y agoI know both languages, did my learnings of Objective-C while porting code from NeXTSTEP into Windows 95, what additional features?
- jb1991 3y agoI won’t teach Swift here, but Google is your friend. You can start by studying all the languages differences with regards to extending existing protocols, and how optional protocol conformance and methods are handled. Swift in many ways (not just with protocols) is like a fix for many headaches that objc provided in these areas. It’s partly why Apple encourages “protocol oriented” programming in swift over “object oriented” because of how rich the feature set is compared to objc.
- pjmlp 3y agoWhich are basically another way to mix protocols and categories from Objective-C. No need for Google, as mentioned, I know both languages. As for protocols not being encouraged in Objective-C, I migth own the wrong NeXTSTEP manuals.
- jb1991 3y agoIt does indeed sound like you have some very old documentation. (as a sibling comment pointed out, swift protocols are typically used for things that in objc you would use inheritance)
- pjmlp 3y agoOld enough to have enough Objective-C protocols and categories in action. The kind of documentation that inspired Java authors. https://cs.gmu.edu/~sean/stuff/java-objc.html https://cs.gmu.edu/~sean/stuff/java-objc.html https://en.wikipedia.org/wiki/Portable_Distributed_Objects https://en.wikipedia.org/wiki/Portable_Distributed_Objects https://en.wikipedia.org/wiki/Distributed_Objects_Everywhere https://en.wikipedia.org/wiki/Distributed_Objects_Everywhere
- jb1991 3y agoQuite a lot has changed in objc since the nextstep days! And a lot more changed as Apple learned from that and built swift. My guess is you don’t know these languages as well as you think if you’re not clear about these differences.