Need Advice: Union or Variant?
jwatson-CO-edu
real.name at colorado.edu
Sat Nov 19 03:50:02 UTC 2022
On Saturday, 19 November 2022 at 03:38:26 UTC, jwatson-CO-edu
wrote:
> Thank you, something similar to what you suggested reduced the
> atom size from 72 bytes to 40.
Oh, based on another forum post I added constructors in addition
to reducing the atom size 44%.
```d
struct Atom{
F_Type kind; // What kind of atom this is
union{
double num; // NMBR: Number value
string str; // STRN: String value, D-string
bool bul; // BOOL: Boolean value
struct{ // ---- CONS: pair
Atom* car; // Left `Atom` Pointer
Atom* cdr; // Right `Atom` Pointer
}
struct{ // ---- EROR: Code + Message
F_Error err; // Error code
string msg; // Detailed desc
}
}
// https://forum.dlang.org/post/omsbr8$7do$1@digitalmars.com
this( double n ){ kind = F_Type.NMBR; num = n; } // make
number
this( string s ){ kind = F_Type.STRN; str = s; } // make
string
this( bool b ){ kind = F_Type.BOOL; bul = b; } // make bool
this( Atom* a, Atom* d ){ kind = F_Type.CONS; car = a; cdr =
d; } // make cons
this( F_Error e, string m ){ kind = F_Type.EROR; err = e; msg
= m; } // make error
}
```
More information about the Digitalmars-d-learn
mailing list