Swift, generics & protocols

Peter Korsten peter at severity-one.com
Wed Jan 18 13:05:50 CET 2017


Op 15-1-2017 om 12:58 schreef Mikael Hansson:
> I’ve tried a million different solutions. Making a protocol for the 
> parser didn’t work as protocols and generics doesn’t mix that well 
> atm, if you specify type in the protocol (by using associatedtype) you 
> can only use the protocol as a type constraint, i.e you can’t, as I 
> need to, both cast to the type and access methods in the protocol.

While I don't understand any of the code you've posted, what you usually 
do is have an abstract base class, derived classes that implement the 
object-specific parsing, and have a factory method that creates the 
appropriate class based on the type of object. I can't see the use for 
generics, unless whatever language you're using doesn't support proper 
derived classes.

- Peter


More information about the music-bar mailing list