Recent

Author Topic: Can I use a Const as the upper bound of an array ?  (Read 3975 times)

Dasun

  • New Member
  • *
  • Posts: 41
Can I use a Const as the upper bound of an array ?
« on: June 28, 2016, 04:58:46 am »
All,

Appreciate any feedback ...

I was wanting to use a constant to set the upperbound of a static array but the compiler does not allow. Is the following - or something similar possible?

Const
    Upperbound : Integer = 10 ;

Type AnArray = Array [1 .. Upperbound ] of Variant ;

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Can I use a Const as the upper bound of an array ?
« Reply #1 on: June 28, 2016, 05:40:51 am »
Code: [Select]
Const
    Upperbound = 10;

Type
  AnArray = Array [1 .. Upperbound ] of Variant ;

The reason for your snippet not working is that using a typed constant is being treated like it is a (special kind of) variable, e.g. using a typed constant allows you to actually change the value of the constant. The compiler cannot handle a type definition based on a number that can vary during execution of the program.

Dasun

  • New Member
  • *
  • Posts: 41
Re: Can I use a Const as the upper bound of an array ?
« Reply #2 on: June 28, 2016, 06:10:07 am »
Doh! Thank you Molly .. and I thought I was being good by specifying the type!!

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Can I use a Const as the upper bound of an array ?
« Reply #3 on: June 28, 2016, 07:53:41 am »
@Dasun:
In theory, pascal being strong typed and all that, you are correct. However, for pascal it is not common to use/have typed constants (only typed variables).

The moment you see/use a typed constant you must think to yourself that something 'fishy' is going on.

You probably won't notice the difference between a typed constant and a variable immediately, especially not when defined in a global scope. The moment you define a typed constant inside a function, procedure, method or otherwise you'll find that a typed constant keeps/restores its last value on entry of such routine while a variable would (re)initializes itself.

You can try the following program to see for yourself how these things behave differently (showing the 'fishy' part, going on inside procedure LocalModify() )
Code: Pascal  [Select][+][-]
  1. program varconsting;
  2.  
  3. const
  4.   globalconst : integer = 10;
  5.  
  6. var
  7.   globalvar   : integer = 10;
  8.  
  9.  
  10. procedure GlobalModify;
  11. var i: Integer;
  12. begin
  13.   WriteLn('globalconst.before = ', globalconst);
  14.   WriteLn('globalvar.before   = ', globalvar);
  15.  
  16.   for i := 1 to 10 do
  17.   begin
  18.     inc(globalconst);
  19.     inc(globalvar);
  20.   end;
  21.   WriteLn('globalconst.after  = ', globalconst);
  22.   WriteLn('globalvar.after    = ', globalvar);
  23. end;
  24.  
  25.  
  26. procedure LocalModify;
  27. const
  28.   localconst : integer = 10;
  29. var
  30.   localvar   : integer = 10;
  31. var
  32.   i: Integer;
  33. begin
  34.   WriteLn('localconst.before  = ', localconst);
  35.   WriteLn('localvar.before    = ', localvar);
  36.  
  37.   for i := 1 to 10 do
  38.   begin
  39.     inc(localconst);
  40.     inc(localvar);
  41.   end;
  42.  
  43.   WriteLn('localconst.after   = ', localconst);
  44.   WriteLn('localvar.after     = ', localvar);
  45. end;
  46.  
  47. begin
  48.   GlobalModify;
  49.   WriteLn;
  50.   GlobalModify;
  51.   WriteLn;WriteLn;WriteLn;
  52.   LocalModify;
  53.   WriteLn;
  54.   LocalModify;
  55. end.
  56.  

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Can I use a Const as the upper bound of an array ?
« Reply #4 on: June 28, 2016, 11:16:44 am »
Object Pascal offers two different sorts of consts: 'proper' consts that are constant (as you might expect), and typed consts that are initialized variables. Naming the latter sort 'const' is a frequent source of confusion for newcomers to Pascal, but it was introduced by Borland, and FPC preserves the same syntax for compatibility with Delphi.

The constant sort of const is always declared using
Code: Pascal  [Select][+][-]
  1. const
  2.   identifier = value;
without a type. The compiler assigns an appropriate  type automatically, although you can override the compiler's choice of type by using a typecast in the declaration. But most situations do not need that level of control over the type of a const.

The initialized variable sort of const, declared using
Code: Pascal  [Select][+][-]
  1. const
  2.   identifier: type = value;
and most often termed a typed constant will always declare an initialized variable (in spite of the const declaration name).
It is possible to treat this initialized variable always as constant by using the compiler directive
Code: Pascal  [Select][+][-]
  1. {$J-} or {$WRITEABLECONST OFF}

Then if you inadvertently assign a new value to your const, the compiler will stop with an error pointing out that you wanted that value to be constant. It is internally still an initialized variable.
For Delphi compatibility the FPC default setting is {$J+}, which means that unless you change the default all typed constants you declare are writeable.

Because typed constants are not true constants the compiler disallows their use where true constants are required (such as in setting the bounds of a static array).

 

TinyPortal © 2005-2018