[fpc-pascal] bounded array types as procedure arguments
Michael Van Canneyt
michael at freepascal.org
Sun Oct 24 10:05:21 CEST 2021
On Sat, 23 Oct 2021, P Padilcdx via fpc-pascal wrote:
> Was trying to declare a procedure with an array argument, e.g. procedure
> p(a: array[1..9] of int64), and the compiler complained as it seems only
> accepts open arrays. If I set a type alias for the array, e.g. type t =
> array[1..9] of int64, then I can declare the procedure p(a: t). This
> behavior seems odd, why couldn’t I declare the procedure with a bounded
> array without having to declare a type alias first?
You can never declare a new type in a procedure header.
For example you also cannot do
procedure (a ; record x,y : integer; end);
procedure (a : (one,two,three));
The reason is type compatibility: the argument type is unknown outside the procedure
and therefor the compiler will never allow you to assign something to it,
since there is no way to check if it is a compatible type, and you will not
be able to do a typecast as there is no type name to typecast with.
An open array is simply an exception for this rule, allowing you to skip the
boundaries of the array.
More information about the fpc-pascal