Configurable Types in Swift
swiftbysundell.com
swiftbysundell.com
And could we achieve much of what we want with protocol-oriented programming crossed with value types if structs could have inheritance? I know that sounds strange.
In short, I totally get the elegance of protocol-oriented programming, but I wonder if for most of its applications a traditional class hierarchy would be more than good enough. I'm not sure inheritance is as bad a word as people make it out to be.
This model doesn't support a single class having multiple base classes very well, whereas this is supported very well by the Swift/Rust approach. Base classes typically also allow shared state between base and derived classes, which can make code brittle and difficult to follow.
Totally agree, but you can have a single base class that adopts multiple protocols or subclasses that adopt multiple protocols. It doesn't have to be an either/or. Inheritance can work together with protocols.
If you're doing something like the Swift standard library, protocol-oriented programming is a huge win. If you're doing a small class hierarchy as you would be to do this example, I don't see the big win.
In the hypothetical example of a note taking app, what bad thing would happen by keeping the switch statement? Adding two lines to a switch statement seems like a trivial step compared to all the other work required to write an importer for something like markdown.
The switch statement is trivially easy to read and understand, more so IMO than replacing it with a FileImportResult enum, a generic FileImporter class, creating static factory methods for each import category, etc. I'd like to understand the benefits of programming in that style over something more simple.