PROGRAM Initial_State_2(INPUT,OUTPUT); { The initial state mechanism allows the specification } { of initial values on types. When used on types, any } { variable, or variant-tag of that type will inherit } { the initial state of the type. The initial state can} { be overriden by specifying another initial state on } { the variable declaration. Heap variables created } { with the NEW procedure also get the initial state of } { their type. Unlike previous versions of VAX Pascal, } { a tag field will be assigned the value in the initial} { state specifier or the "C" parameter of a NEW call. } { } { By specifying an initial state on a type, you can } { ensure that all variables of that type are } { initialized to a known value. In combination with } { separate compilation, this can be used to make a safe} { data abstraction mechanism. } TYPE Initially_42 = INTEGER VALUE 42; Initially_43 = Initially_42 VALUE 43; Data_Record = RECORD Initialized : BOOLEAN; F1, F2, F3 : INTEGER; END VALUE [Initialized : FALSE; F1,F2,F3 : 0]; Variant_Record = RECORD CASE Tag : Initially_42 OF 1: (One : INTEGER VALUE 12); 42:(Forty_Two : REAL VALUE 3.14); OTHERWISE (Unknown : BOOLEAN VALUE FALSE); END; VAR I : Initially_42; J : Initially_43; K : Initially_42 VALUE 44; P : ^Data_Record; V : ^Variant_Record; BEGIN { I and J have the initial state of their types. } { K overrode the initial state of the type. } WRITELN(I,J,K); { Using NEW on a Data_Record will initialize the } { heap-allocated record. } NEW(P); WRITELN(P^.Initialized,P^.F1,P^.F2,P^.F3); { Using NEW on a Variant_Record will fill in the } { tag field since its type has an initial state. } { Since the tag was defined at the variable's } { creation point, so is the variant. Since the } { variant is active at the creation point, the } { fields in the respective variant take on their } { initial states. } NEW(V); WRITELN(V^.Tag, V^.Forty_Two); NEW(V,1); WRITELN(V^.Tag, V^.One); NEW(V,2); WRITELN(V^.Tag, V^.Unknown); END.