Next: , Previous: Strings in BP and GPC, Up: Borland Pascal



7.11 Typed Constants

GNU Pascal supports Borland Pascal's “typed constants” but also Extended Pascal's initialized variables:

     var
       x: Integer value 7;

or

     var
       x: Integer = 7;

When a typed constant is misused as an initialized variable, a warning is given unless you specify --borland-pascal.

When you want a local variable to preserve its value, define it as static instead of using a typed constant. Typed constants also become static automatically for Borland Pascal compatibility, but it's better not to rely on this “feature” in new programs. Initialized variables do not become static automatically.

     program StaticDemo;
     
     procedure Foo;
     { x keeps its value between two calls to this procedure }
     var
       x: Integer = 0; attribute (static);
     begin
       WriteLn (x);
       Inc (x)
     end;
     
     begin
       Foo;
       Foo;
       Foo;
     end.

For records and arrays, GPC supports both BP style and Extended Pascal style initializers. When you initialize a record, you may omit the field names. When you initialize an array, you may provide indices with a :. However, this additional information is ignored completely, so perhaps it's best for the moment to only provide the values ...

     program BPInitVarDemo;
     const
       A: Integer = 7;
       B: array [1 .. 3] of Char = ('F', 'o', 'o');
       C: array [1 .. 3] of Char = 'Bar';
       Foo: record
         x, y: Integer;
       end = (x: 3; y: 4);
     begin
     end.