
Swift Functional Programming
By :

In Chapter 3, Types and Type Casting we talked about Abstract versus Concrete types. Type erasure is a process to make abstract types such as Generics concrete.
Why we would want to do it? The answer is because we want to write code against contracts; in other words, we want to prefer composition over inheritance. Also, sometimes we would want to be more flexible with the types. This concept may sound complicated, so let's continue our example from the previous section to understand why and how we would create type-erased structures.
First, we want to examine if we can use CustomView
type and create an array with CustomView
elements. We will create a CustomView
:
struct CustomDisabledButton: CustomView { typealias ViewType = Button func configure(view with: ViewType) { // configure the view print("Disabled button") } }
So far, we've created two CustomViews
; the first one represents a custom enabled button and the second one represents a disabled...