The Storage variable is uninitialised. Therefore for each entry the same variant (the default Desk) is stored and the discriminant check fails.
Now initialisation is apt to get very tedious as we shall see presently if not done in a smart way. First we add a component denoting whether there is stored anything at all.
type Document (Key: Key_Template := Desk) is record Empty: Boolean; ... -- Rest as before end record;
The package body gets an executable part (executed during elaboration):
package body Archive is – Declarative part as before begin for Key in Key_Template loop case Key is when Desk => Storage (Key) := (Key => Desk, Empty => True, Confidential => ...); when Safe => Storage (Key) := (Key => Safe, Empty => True, Secret => ...); when ... => ... end case; end loop; end Archive;
To our dismay, with this method in a way we have to repeat the whole record declaration. If we have many variants, this is getting really awkward.
Fortunately all this is unnecessary if we add the correct default values to the record declaration:
type Document (Key: Key_Template := Desk) is record Empty: Boolean := True; ... -- Rest where applicable with appropriate default values end record;
Initialising now is really simple.
for Key in Key_Template loop declare Letter: Document (Key); -- Letter: constant Document (Key) := ???; begin Storage (Key) := Letter; end; end loop;
Note the variable declaration without an initial value - a constant would need an initial value and we would be back at the problem how to denote it. (By the way: Declarations without initial value are strictly disallowed by some coding standards, especially when the variable is never assigned a value, as is the case here! But as we have seen, sometimes seemingly obvious nonsense is unavoidable and - if done in the correct way - perfectly legal.)
|Contents||© Copyright 1998 C.K.W. Grein|