struct Info{
char name[30];
int age;
struct address{
char area_name[39];
int house_no;
char district[39];
};
};
I can refer to "house_no" so long as I qualify the reference to it with Info.Address. But how can one do that in Ada, nest structure declarations in that way? That's the question I'm struggling to find an answer too...
Oberon is similar to Ada for these types of declarations. In Oberon this is written as:
TYPE
Info = RECORD
name: ARRAY 30 OF CHAR;
age: INTEGER;
address:
RECORD
areaName: ARRAY 39 OF CHAR;
houseNo: INTEGER;
district: ARRAY 39 OF CHAR;
END;
END;
However, I would be more likely (depending on the exact requirements) to write this as:
TYPE
Name = ARRAY 30 OF CHAR;
Location = ARRAY 39 OF CHAR;
Address = RECORD
houseNo: INTEGER;
areaName, district: Location;
END;
Person = RECORD
name: Name;
age: INTEGER;
address: Address;
END;
In Oberon entire records can be assigned so, given these declarations you could write:
VAR
twin1, twin2: Person;
...
...
twin2 := twin1;
twin1.name := "John Smith";
twin2.name := "Jane Smith";
...
...
Thank you this is most interesting.
Here is the current syntax for "defining a new type" (or rather "alias" might be the better term).
type outer structure;
cpu_id bin(31);
inner structure;
cpu_mode bin(31);
cpu_rev bin(15);
large as Header;
flag_table structure;
root pointer;
depth fixed bin(15);
structure structure;
goofy pointer;
end;
end;
label string(64);
end;
owner string(64);
speed(100) as baud_rates;
counter string(32);
end;
This works, and it and several complex variants now parse without issue and build the expected parse tree.
I can see that making say 'flag_table' an array, is entirely legitimate, it is describing the layout of some structured storage, so an array of members be they scalar fields or structures is entirely sensible.
The conceptual concern I have is that 'outer' (the main structure itself) is not itself a structure but a 'type' name and so allowing that to be subscripted is a bad idea for the reasons I mention above.
So its just niggling me that the type is defined as "outer structure" and embedded struct instance like "flag_table structure" use the same notation, but then again the former is prefixed by "type".
Furthermore I am inclined to disallow the declaration of structure instances like this, to only allow a "type" or "alias" to be defined this way and then one must use that alias to declare instances. But I don't want to impose that rule when inserting sub structures inside some larger structure, so I don't want to force the developer to define a "type" for an embedded structure if they don't want to, only for the outermost structure.
So I'm trying to rationalize all this in a way that is completely free of contrived syntactic rules...
By the way the "name
as othername" is the way we declare "name"
as being of user defined type "othername".