Recursive data-types
Meta via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Sep 27 08:45:18 PDT 2014
On Saturday, 27 September 2014 at 11:26:33 UTC, ponce wrote:
> I'm dabbling with Scheme interpreter and ultimately I would
> need to declare the following types.
>
> --------------
>
> struct Function
> {
> Environment env;
> Atom params;
> Atom body_;
> }
>
> // An atom is either a string, a double, a symbol, a function
> or a list of atoms
> alias Atom = Algebraic!(string, double, Symbol, Function,
> This[]);
>
> --------------
>
> These definitions can't work since Function and Atom need each
> other in this recursive definition.
>
> How to get out of this trap?
> Do I have to drop Algebraic and go back to manual tagged unions?
You can also use a pointer to a Function. Basically, any
indirection will solve this problem, whether it be via class or
pointer.
struct Function
{
Environment env;
//Either do this:
Atom* params;
Atom* body_;
}
//Or this //Now a pointer
alias Atom = Algebraic!(string, double, Symbol, Function*,
This[]);
Also, you might want to use This* instead of This[], unless you
want an Atom to be able to contain a whole array of other atoms.
More information about the Digitalmars-d-learn
mailing list