4 ms·
Yeah, the same code would look much clearer in a language with union types. Heck even Swift cribbed them. switch resp { case Result(val, meta): doStuff
by lambdasquirrel 5y ago
Yeah, the same code would look much clearer in a language with union types. Heck even Swift cribbed them.
switch resp {
case Result(val, meta):
doStuff(val)
case Error(msg, code):
logger.main.debug(msg)
}
- dragonwriter 5y agoYou mean sum types, not union types (TypeScript has union types, your example uses sum types.)
- marcosdumay 5y agoAn union type is the C attempt to implement something like sum types, that other languages extend a bit to literally implement sum types. Those two words live on different contexts, as unions are about a memory usage pattern, and sum types are about conceptual software design, but they often go together on the same language feature.
- munificent 5y ago> An union type is the C attempt to implement something like sum types Sum types, unions (in the C sense), and union types are three distinct concepts. Wikipedia's article confusingly merges the latter two concepts together but they're different thing. TypeScript[1] and Scala[2] have union types, but they have nothing like C's notion of "unsafely reuse the same bits in memory to be interpreted as different things". [1]: https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#union-types https://www.typescriptlang.org/docs/handbook/2/everyday-type... [2]: https://dotty.epfl.ch/docs/reference/new-types/union-types.html https://dotty.epfl.ch/docs/reference/new-types/union-types.h...
- lambdasquirrel 5y agoI did mean sum types. Thank you for the correction.