On Thursday, 17 November 2022 at 21:05:43 UTC, H. S. Teoh wrote:
Question:
**Where do I begin my consolidation of space within `Atom`?
Do I use
unions or variants?**
In this case, since you're already keeping track of what type
of data is being stored in an Atom, use a union:
struct Atom {
F_Type kind;
union { // anonymous union
Atom* car; // ----------------- Left `Atom` Pointer
Atom* cdr; // ----------------- Right `Atom` Pointer
double num; // ----------------- Number value
string str; // ----------------- String value, D-string
underlies
bool bul; // ----------------- Boolean value
F_Error err = F_Error.NOVALUE; // Error code
}
}
Use Variant if you don't want to keep track of the type
yourself.
T
Thank you! This seems nice except there are a few fields that
need to coexist.
I need {`car`, `cdr`} -or- {`num`} -or- {`str`} -or- {`bul`}.
`err` will be outside the union as well because I have decided
that any type can have an error code attached. As in an error
number (other than NaN) can be returned instead of reserving
certain numbers to represent errors. Imagine if there was NaN
for every datatype.