All classes, structs and enums in Swift are named and they are concrete in the sense that there are no abstract members, like abstract methods or properties. Especially for classes with very little functionality, it would sometimes be handy to have a lightweight approach to define a single instance of a class or struct. This post introduces a Swift-specific design pattern for creating such lightweight anonymous class and struct instances.


Many object-oriented languages support the concept of abstract classes as a means to define classes that are incomplete from an implementation perspective while providing a common supertype for all complete implementations. Abstract classes do not exist in Swift 2. This post explains how programmers can work around this limitation. It explains how abstract classes can be encoded and extended. The post will also show that there are interesting limitations in the type system of Swift 2 which lead to further complications when dealing with generic abstract classes, i.e. abstract classes with type parameters.