I've got code that looks like this:
class Base {
func launch(code1: Int, code2: Int) -> Bool { return false }
}
class A: Base {}
class B: Base {}
class C: Base {}
func trynext(obj: Base) -> Base? {
switch obj {
case is A: return B()
case is B: return C()
default: return nil
}
}
Basically, I've got a lot (like 20) of subclasses of a common base class and I need to go through them one by one. These subclasses represent parsers and I'm trying them one after another to discover which parser correctly parses some data.
If I fail on a parse, I call a function trynext to return the "next parser" to try. You can imagine that this switch statement can get unyieldly if the constructors take arguments (all subclasses take the same arguments), and the more subclasses there are, etc.
Is there any way I can streamline this code by putting the classes into some sort of array and loop through it somehow? The idea is to reduce the boilerplate so that I end up using a structure like [A, B, C] that implies the subclasses to try and the order to try them in.