▲ | ahartmetz 3 days ago | |
I wonder why sum types are named like that. They are not the sum of their possible states in a way that I would recognize, more like an exclusive-or or one of out many. At some point I came up with an explanation, but I forgot it again. If you need to explain why something is simple, it's not simple. | ||
▲ | duckfan77 3 days ago | parent [-] | |
The number of valid options is the sum of the valid option for each variant. This is also why product types are called product types. If you have a struct (product type) containing an 8 bit integer and a boolean, the struct has 512 possible states. 256 for the integer, times 2 for each value of the boolean. If you have a sum type where one variant is an 8 bit integer, and the other is a boolean, you have 258 possible states. 256 for when it's an integer, and 2 for when it's a boolean. Sum types add the options of each variant, product types multiply the options for each item in it. |