4 ms·
I mostly do iOS work, and I find ADTs useful for representing UI state machines. For example, if I have a view controller (screen) that async loads data, I can
by mlex 6y ago
I mostly do iOS work, and I find ADTs useful for representing UI state machines.
For example, if I have a view controller (screen) that async loads data, I can have an enum State (in Swift enums have associated values), with the following cases:
- An initial state with nothing
- A loading state that contains the associated Promise that represents the network request (which I can cancel)
- A fully-loaded state with the data model I got back from the network
- An error state containing an error object I can display to the user
Also, the most common use case for an ADT in Swift is representing Optional, which is when you have a nullable object.