Swift TIL #2: Preserving backward compatibility while refactoring enums with associated values

Let’s say you have somewhere in your code such enum with associated values:

The only difference between serverError and noInternetConnection cases is title and description that we will show to user. Also each of them has showRetryButton: Bool associated value. Then we decide to refactor it a little bit: get rid of serverError and noInternetConnection cases and introduce more flexible error case:

Even though we still have lots of usages of old serverError and noInternetConnection in our code and it will be annoying to substitute them with error at once:

But you can skip type name(like you do when instead of ViewModel.serverError(showRetryButton: true) you just write .serverError(showRetryButton: true)) in any place where type inference can determine implied type. It means that you can substitute serverError and noInternetConnection with static methods like here:

Which allows us to use both old/new variants at the same time:

However you still have to refactor usages of old cases in switch and if case statements.

P.S. Also I think it should work for cases without associated values, but in this case its better to use static property than static method.