3 ms·
Yes. Product types (i.e. tuples) force you to consider all possible combinations of things, and the types have to be the same across all variants. This means t
by truculent 5y ago
Yes. Product types (i.e. tuples) force you to consider all possible combinations of things, and the types have to be the same across all variants.
This means that there are certain states that may be impossible in practice, but possible to represent in your data. This can lead to unnecessary code at best, or undefined behaviour and bugs at worst.
For example, let's say we want to model loading data from a server.
With ADTs:
type WebData
= NoRequest
| Loading
| Failure HttpError
| Success MyData
With product types only (and enums), how would we reconcile that the request could return an error _or_ data?
We could try something along the lines of:
type WebDataStatus
= NoRequest
| Loading
| Failure
| Success
type WebData = (WebDataStatus, Maybe HttpError, Maybe MyData)
Where the error and data fields are optional.
But in this case, it would be possible to have both error _and_ data fields populated. Not ideal!
- bradrn 5y agoIt gets worse — technically speaking, your second ‘WebDataStatus’ is also a sum type! Admittedly most non-functional programmers would call it an enumeration, but nonetheless, a language without support for any sum types at all would also necessarily need to lack enumerations. (I therefore prefer to think of most non-functional programming languages as having an incomplete implementation of sum types rather than having no sum types.)
- truculent 5y agoYes, agreed!