Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2021-10

bounded array types as procedure arguments

3 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

P Padilcdx

Hello,

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?

Thank you and apologies if this has been asked before. I just joined the mailing list.

Michael Van Canneyt
to P Padilcdx
P Padilcdx wrote 8 lines
Hello, 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);
or
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.

Michael.

P Padilcdx
to Michael Van Canneyt

Thank you for the clear explanation.

Show quoted text 33 lines
On Oct 24, 2021, at 1:05 AM, Michael Van Canneyt via fpc-pascal wrote: On Sat, 23 Oct 2021, P Padilcdx via fpc-pascal wrote: > Hello, > > 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); or 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. Michael._______________________________________________ fpc-pascal maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal