Recent

Author Topic: Array of Integer as parameter accepts Integers  (Read 1648 times)

Fibonacci

  • Hero Member
  • *****
  • Posts: 613
  • Internal Error Hunter
Array of Integer as parameter accepts Integers
« on: September 27, 2024, 12:31:10 pm »
Seems like an Integer can be passed to a procedure that accepts an Array of Integers. Why is that?

Code: Pascal  [Select][+][-]
  1. uses SysUtils;
  2.  
  3. procedure test(a: array of integer);
  4. var
  5.   i: integer;
  6. begin
  7.   writeln('len  = ', length(a), ', size = ', sizeof(a):3);
  8.   for i := 0 to high(a) do writeln(i, ' = ', inttohex(a[i]));
  9. end;
  10.  
  11. begin
  12.   //test(1);           // project1.lpr(14,9) Error: Incompatible type for arg no. 1: Got "ShortInt", expected "{Open} Array Of LongInt"
  13.   test(integer(1));
  14.   //test($ffff);       // project1.lpr(16,13) Error: Incompatible type for arg no. 1: Got "Word", expected "{Open} Array Of LongInt"
  15.   test(integer($ffff));
  16.   test($ffff+1);
  17.   test(123456789);
  18.  
  19.   test([1, $ffff, $ffff+1, 123456789]);
  20.  
  21.   readln;
  22. end.

Quote
len  = 1, size =   4
0 = 00000001
len  = 1, size =   4
0 = 0000FFFF
len  = 1, size =   4
0 = 00010000
len  = 1, size =   4
0 = 075BCD15
len  = 4, size =  16
0 = 00000001
1 = 0000FFFF
2 = 00010000
3 = 075BCD15

MarkMLl

  • Hero Member
  • *****
  • Posts: 8039
Re: Array of Integer as parameter accepts Integers
« Reply #1 on: September 27, 2024, 12:41:13 pm »
Seems fairly logical to me: you've declared the parameter to accept any number of integers in an array. This might be mode-specific: what compiler mode are you using (and what compiler version etc.)?

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

ASerge

  • Hero Member
  • *****
  • Posts: 2340
Re: Array of Integer as parameter accepts Integers
« Reply #2 on: September 27, 2024, 01:54:54 pm »
Seems like an Integer can be passed to a procedure that accepts an Array of Integers. Why is that?
Yes, this is not reflected in the documentation.
For example from Delphi 7 documentation:
Quote
Within the body of a routine, open array parameters are governed by the following rules.
  • They are always zero-based. The first element is 0, the second element is 1, and so forth. The standard Low and High functions return 0 and Length - 1, respectively. The SizeOf function returns the size of the actual array passed to the routine.
  • They can be accessed by element only. Assignments to an entire open array parameter are not allowed.
  • They can be passed to other procedures and functions only as open array parameters or untyped var parameters. They cannot be passed to SetLength.
  • Instead of an array, you can pass a variable of the open array parameter's base type. It will be treated as an array of length 1.
I have bold the last point. FPC extended this action not only for variables, but also for constants, but only those for which the type is set.

Fibonacci

  • Hero Member
  • *****
  • Posts: 613
  • Internal Error Hunter
Re: Array of Integer as parameter accepts Integers
« Reply #3 on: September 27, 2024, 02:15:58 pm »
Kudos ASerge, always like your responses

PascalDragon

  • Hero Member
  • *****
  • Posts: 5764
  • Compiler Developer
Re: Array of Integer as parameter accepts Integers
« Reply #4 on: September 28, 2024, 04:26:15 pm »
Seems like an Integer can be passed to a procedure that accepts an Array of Integers. Why is that?

Open array parameters are essentially a pointer to the first element and an additional argument with the high value of the array. It's logical that one can also pass single elements as essentially a single element array. This is compatible to Delphi and works in any mode that supports open array parameters. If it's not documented then someone please file a bug report against the documentation.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11947
  • FPC developer.
Re: Array of Integer as parameter accepts Integers
« Reply #5 on: September 28, 2024, 05:06:06 pm »
It is not just accepting one element, but more the omission of the [] ?

TRon

  • Hero Member
  • *****
  • Posts: 3650
Re: Array of Integer as parameter accepts Integers
« Reply #6 on: September 28, 2024, 05:20:21 pm »
@marcov
exactly that.

Because if that would to be desired behaviour then why
Code: Pascal  [Select][+][-]
  1. Foo();
Is considered an error.

Should it not be considered an empty array by that same logic ?
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11947
  • FPC developer.
Re: Array of Integer as parameter accepts Integers
« Reply #7 on: September 28, 2024, 05:38:28 pm »
Delphi Seattle refuses the example code with "constants cannot be used as open array arguments".  For all four of the ones without [].

jamie

  • Hero Member
  • *****
  • Posts: 6735
Re: Array of Integer as parameter accepts Integers
« Reply #8 on: September 28, 2024, 06:27:00 pm »
Don't see a problem here?

The compiler is going to build an array for the parameter in both cases. The "[..]" only needed to not confuse the compiler between parameter count and element count entries beyond one element for the array.

I believe other languages do the same in this case that have managed arrays.


The only true wisdom is knowing you know nothing

simone

  • Hero Member
  • *****
  • Posts: 626
Re: Array of Integer as parameter accepts Integers
« Reply #9 on: September 28, 2024, 07:21:39 pm »
I had already reported this behavior here:

https://forum.lazarus.freepascal.org/index.php/topic,58489.msg435598.html#msg435598

I reiterate my perplexity, given the strong typing of pascal. A scalar parameter of type integer is not an array of integers with length =1. This behavior is justified only by implementation reasons. But I know I'm in the minority..



Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

TRon

  • Hero Member
  • *****
  • Posts: 3650
Re: Array of Integer as parameter accepts Integers
« Reply #10 on: September 28, 2024, 07:24:26 pm »
@marcov:
thank you for the verifcation

@jamie:
So in your view there is not an issue with the following ?

Code: Pascal  [Select][+][-]
  1. procedure Foo(a: array of integer);
  2. begin
  3. end;
  4.  
  5. procedure Foo(a: integer);
  6. begin
  7. end;
  8.  

I do see a problem there but perhaps that is just me ?

@simone:
you might perhaps be in the minority but I would call it an abomination as the example clearly shows that you can't rely on the behaviour because as soon as you overload your code behaves differently (and without any warning or indication). imho useless and dangerous behaviour.
« Last Edit: September 28, 2024, 07:39:53 pm by TRon »
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

simone

  • Hero Member
  • *****
  • Posts: 626
Re: Array of Integer as parameter accepts Integers
« Reply #11 on: September 28, 2024, 08:38:24 pm »
Tron, I agree. I'm glad there's at least one authoritative forum member who thinks the same way I do on this point.
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

440bx

  • Hero Member
  • *****
  • Posts: 4749
Re: Array of Integer as parameter accepts Integers
« Reply #12 on: September 28, 2024, 08:45:33 pm »
That Foo example really shows why the compiler should _not_ accept an integer variable as a single element array.  It is ambiguous.

The compiler chooses the Foo with the integer parameter but, there is no indication of any kind that is the choice it makes.

The compiler should require the [] to eliminate the ambiguity its absence creates.  The fact that it doesn't require it should be considered a bug.



@simone,

add 1 (me) to the count of members that agree with you.

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

jamie

  • Hero Member
  • *****
  • Posts: 6735
Re: Array of Integer as parameter accepts Integers
« Reply #13 on: September 28, 2024, 09:09:17 pm »
No, I don't see a problem with it, Compiler still generates an array which can be tested in code.

the brackets are only there to enclose multiple entries. either way, an array is created.

I'll step out the spotlight on this one because I would rather the compiler allow reduce syntax as much as possible.

The only true wisdom is knowing you know nothing

alpine

  • Hero Member
  • *****
  • Posts: 1303
Re: Array of Integer as parameter accepts Integers
« Reply #14 on: September 28, 2024, 09:16:13 pm »
@simone
Count me also.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

 

TinyPortal © 2005-2018