Now that we have seen how to implement generic functions, let's have a look at how to implement it with other data structures such as enums.
One of the popular addition swift programmers often like to have in their tool chain is the Result or Either types. These two types are best represented with enum as they can only hold a finite number of different values (two).
Result is used when a function can resolve with either a success value or an error (Error). Either is used when a function can resolve with two different values (a left value or a right value):
enum Result<T> {
case success(T), error(Error)
}
enum Either<T, U> {
case left(T), right(U)
}
Both of these implementations are unconstrained generics, unlike the previous example:
- Result is an enum with a single generic parameter, T.
- Either is an enum with two generic parameters, T and U.
Now that we've seen implementations of generics classes and functions, we can go take a look at an example of conditional conformance.