by elnygren on 8/10/21, 12:10 PM with 21 comments
by lalaithion on 8/11/21, 2:52 PM
enum Bool {
True,
False
}
enum Status {
Waiting,
Successful,
Failed
}
We can combine them into a product type, like a tuple, or a sum type, like a union. type Product = (Bool, Status)
type Sum = Bool | Status
Now, we ask ourselves, what are the valid values of type Product? (True, Waiting)
(True, Successful)
(True, Failed)
(False, Waiting)
(False, Successful)
(False, Failed)
There are two Bool values, and three Status values, so there are 2×3=6 values for the product of Bool and Status. Now, what about the Sum type? True
False
Waiting
Successful
Failed
We have 2+3=5 total values for the sum of Bool and Product.Now, obviously, this math doesn't quite work out for types with infinite values, like strings or bigints or arrays, but that's where the analogy comes from.
If you extend this further, you can even figure out what an exponential type would be: the exponential type Bool^Status is a function that takes a Status as its argument and returns a Bool.
by benrbray on 8/11/21, 2:06 PM
by g_delgado14 on 8/11/21, 4:28 PM
by inssein on 8/12/21, 3:31 AM
We use another class based concept on top at work where you can compose multiple pieces of remote data together, but it really only works well for the happy path.
by ramesh31 on 8/11/21, 5:35 PM