On Thu, Nov 17, 2022 at 08:54:46PM +0000, jwatson-CO-edu via 
Digitalmars-d-learn wrote:
[...]
> ```d
> enum F_Type{
>     CONS, // Cons pair
>     STRN, // String/Symbol
>     NMBR, // Number
>     EROR, // Error object
>     BOOL, // Boolean value
>     FUNC, // Function
> }
> 
> struct Atom{
>     F_Type  kind; // ---------------- What kind of atom this is
>     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
> }
> 
> ```
> 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

-- 
An elephant: A mouse built to government specifications. -- Robert Heinlein

Reply via email to