5 ms·
Yes, that is basically correct, but I think your assertions that "you cover all the cases" and "valuable in catching bugs" are not correct. For example, in C,
by cuddlecake 7y ago
Yes, that is basically correct, but I think your assertions that "you cover all the cases" and "valuable in catching bugs" are not correct.
For example, in C, you can define a Union type that can hold either a string or an integer, declare a variable of that type and initialize it with a string, and then simply assert that it is an integer.
I think that with your assertion of "valuable in catching bugs" you are referring to Sum Types (often called tagged unions). For Sum Types, if a language supports this concept, it usually also forces you to cover all cases at compile time, whereas with plain unions, you can shoot yourself in the foot, if you're willing to do so.
- rapind 7y agoYes I'm referring specifically to tagged unions in a typed (type safe) language. That's what I thought you we're referencing when you said "var input: string | number". Excuse my pedantry if I misunderstood.