21 ms·
The computer science name for that is sum type, the (or maybe just the simplest) implementation a tagged union (maybe I'm making up that distinction; Wikipedia
by Someone 12y ago
The computer science name for that is sum type, the (or maybe just the simplest) implementation a tagged union (maybe I'm making up that distinction; Wikipedia thinks they are synonyms (http://en.wikipedia.org/wiki/Sum_type) http://en.wikipedia.org/wiki/Sum_type)). You can do ~the same~ in C with a struct and an union:
typedef sruct s
{
int type;
union
{
int i;
float f;
double d;
} value;
}
But there, it is up to you to make sure that you only read the int/float/double if the 'type' field indicates a int/float/double was stored. Higher-level languages enforce that you can only read the union as being of type T if you wrote it as type T.