3 ms·
> both But only one of them would make sense at a given moment, so why waste memory?
by Koshkin 8y ago
> both
But only one of them would make sense at a given moment, so why waste memory?
- AnimalMuppet 8y agoSo that at any given moment, you know which one you have.
- Koshkin 8y agoThat requires 1 bit of information, and there is often a way of embedding that bit in the value itself; but the general technique of representing alternatives is called “a tagged union.” (You can also hear the fancy terms “the product type” for the struct and “the sum type” for the union.)
- lixtra 8y agoAccessing that one bit can be as expensive as passing an int around.
- kllrnohj 8y agoYour example doesn't tell me which I have. It's assuming that err = 0 means success, but that's an assumption not a contract of the type. By comparison some something like https://github.com/oktal/result https://github.com/oktal/result will tell you if it's a success or error without any magic error codes that actually mean success. You could do this with a type bit + union in C, it's just more painful without templates
- Gibbon1 8y ago> so why waste memory? In c it'll be either in a register or on the stack.