Reputation:
I have a simple question but I can't find the answer.
Is it possible in Ada to have 2 types containing a component of each other's type?
Here is a simple example of what I want to do:
type Toto is record
T: Titi;
end record;
type Titi is record
T: Toto;
end record;
It is not working in this way but is it possible to make something equivalent?
Upvotes: 1
Views: 1552
Reputation: 251
You cannot do this: how should an instance of Toto ever be created? It has to contain an instance of Titi, which in turn contains a Toto, and so on.
However, something similar is possible:
type Titi;
type Toto is record
T : access Titi;
end record;
type Titi is record
T : Toto;
end record;
In that case, Toto only contains a reference/a pointer to a record of type Titi, not an actual Titi. In order to find optimal type declarations, think carefully about what you want to represent with these types, and what the relationships really are. I am certain you will find that what you presented in your question does not accurately represent your problem. Instead, at least one of Toto.T and Titi.T needs to be represented by an access value -- possibly both. You do not say what the problem at hand is, so I do not know what would be the best (most fitting) type declaration here.
Upvotes: 3
Reputation: 3211
You have to forward declare Titi. I think the following should do it:
type Titi;
type Toto is record
T: Titi;
end record;
type Titi is record
T: Toto;
end record;
Upvotes: 0