Sign in Join

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

Mailing lists / fpc-devel / 2012-06

Scalar value is accepted as an argument for an open array parameter

11 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

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

Sven Barth
to Alexander Klenin
Alexander Klenin wrote 10 lines
The following code compiles both with : procedure F(const A: array of ShortInt); begin end; begin F(1); end. I think this is a bug in FPC, but if not -- it should be documented here: http://freepascal.org/docs-html/ref/refsu59.html#x157-16700014.4.5 I am willing to file an issue one way or another.

AFAIK this is by intension (this was mentioned some weeks ago on one of the lists, I can't find it though...).

Regards,
Sven

Reply by email

Marc Weustink
to Alexander Klenin: The following code compiles both with : procedure F(const A: array of ShortInt); begin…
Alexander Klenin wrote 9 lines
The following code compiles both with : procedure F(const A: array of ShortInt); begin end; begin F(1); end. I think this is a bug in FPC, but if not -- it should be documented here: http://freepascal.org/docs-html/ref/refsu59.html#x157-16700014.4.5 I am willing to file an issue one way or another.

Unfortunately Delphi (6) accepts this too. It took me some time at work to figure out why some code suddenly behaved weird. (I changed a integer argument to array of integer and assumed the compiler would help me to find the places where I still passed a single integer)

Marc

Reply by email

Alexander Klenin
to Marc Weustink
Marc Weustink wrote
Unfortunately Delphi (6) accepts this too. It took me some time at work to figure out why some code suddenly behaved weird. (I changed a integer argument to array of integer and assumed the compiler would help me to find the places where I still passed a single integer)

I suggest this "feature" to be disabled in objfpc mode then.

Alexander S. Klenin

Reply by email

Sven Barth
to Alexander Klenin
Alexander Klenin wrote 8 lines
On Fri, Jun 8, 2012 at 6:08 PM, Marc Weustink wrote: > Unfortunately Delphi (6) accepts this too. It took me some time at work to > figure out why some code suddenly behaved weird. (I changed a integer > argument to array of integer and assumed the compiler would help me to find > the places where I still passed a single integer) I suggest this "feature" to be disabled in objfpc mode then.

This would not be backwards compatible, thus the answer to this request is most likely: No.

Regards,
Sven

Reply by email

Alexander Klenin
to Sven Barth
Sven Barth wrote 5 lines
> I suggest this "feature" to be disabled in objfpc mode then. > This would not be backwards compatible, thus the answer to this request is most likely: No.

On the other hand, FPC has an established history of breaking backwards compatibility in favor of type cheking.
In my opinion this breakage is substantially more acceptable than some previous ones.

Alexander S. Klenin

Reply by email

Skybuck Flying
to Marc Weustink: Unfortunately Delphi (6) accepts this too. It took me some time at work to figure out why…

-----Original Message-----

Earlier message 25 lines
From: Marc Weustink Sent: Friday, June 08, 2012 9:08 To: FPC developers' list Subject: Re: [fpc-devel] Scalar value is accepted as an argument for an openarray parameter On 8-6-2012 8:18, Alexander Klenin wrote: > The following code compiles both with : > > procedure F(const A: array of ShortInt); begin end; > begin F(1); end. > > I think this is a bug in FPC, but if not -- it should be documented here: > http://freepascal.org/docs-html/ref/refsu59.html#x157-16700014.4.5 > > I am willing to file an issue one way or another. " Unfortunately Delphi (6) accepts this too. It took me some time at work " Delphi XE2 does not ;) Bye, Skybuck :)

Reply by email

Sven Barth
to Skybuck Flying
Skybuck Flying wrote 24 lines
-----Original Message----- From: Marc Weustink Sent: Friday, June 08, 2012 9:08 To: FPC developers' list Subject: Re: [fpc-devel] Scalar value is accepted as an argument for an openarray parameter On 8-6-2012 8:18, Alexander Klenin wrote: > The following code compiles both with : > > procedure F(const A: array of ShortInt); begin end; > begin F(1); end. > > I think this is a bug in FPC, but if not -- it should be documented here: > http://freepascal.org/docs-html/ref/refsu59.html#x157-16700014.4.5 > > I am willing to file an issue one way or another. " Unfortunately Delphi (6) accepts this too. It took me some time at work " Delphi XE2 does not ;)

Delphi XE does not accept constants either, but scalar variables. So the following compiles:

=== source begin ===

program openarrayscalar;
{$apptype console}
procedure Test(aArgs: array of LongInt);
var
  i: LongInt;
begin
  for i in aArgs do
    Writeln(i);
end;
var
  z: LongInt;
begin
  z := 42;
  Test(z);
  Readln;
end.

=== source end ===

The Delphi help also mentions this here:
http://docwiki.embarcadero.com/RADStudio/en/Parameters_%28Delphi%29#Open_Array_Parameters

=== qoute begin ===

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.

=== qoute end ===

Constant values and untyped constants can indeed not be used as argument for open array parameters, but typed constants can.

Regards,
Sven

Reply by email

Skybuck Flying
to Sven Barth

-----Original Message-----

Earlier message 68 lines
From: Sven Barth Sent: Sunday, June 10, 2012 18:23 To: Subject: Re: [fpc-devel] Scalar value is accepted as an argument for anopenarray parameter On 10.06.2012 00:22, Skybuck Flying wrote: > > > -----Original Message----- From: Marc Weustink > Sent: Friday, June 08, 2012 9:08 > To: FPC developers' list > Subject: Re: [fpc-devel] Scalar value is accepted as an argument for an > openarray parameter > > On 8-6-2012 8:18, Alexander Klenin wrote: >> The following code compiles both with : >> >> procedure F(const A: array of ShortInt); begin end; >> begin F(1); end. >> >> I think this is a bug in FPC, but if not -- it should be documented here: >> http://freepascal.org/docs-html/ref/refsu59.html#x157-16700014.4.5 >> >> I am willing to file an issue one way or another. > > " > Unfortunately Delphi (6) accepts this too. It took me some time at work > " > > Delphi XE2 does not ;) " Delphi XE does not accept constants either, but scalar variables. So the following compiles: <snip> " Constant values and untyped constants can indeed not be used as argument for open array parameters, but typed constants can. " // Indeed: program openarrayscalar; {$apptype console} const // value = 42; // not allowed value : integer = 42; // allowed procedure Test(aArgs: array of LongInt); var i: LongInt; begin for i in aArgs do Writeln(i); end; begin Test(value); Readln; end. Bye, Skybuck.

Reply by email

Alexander Klenin
to Skybuck Flying
Skybuck Flying wrote 6 lines
> Unfortunately Delphi (6) accepts this too. It took me some time at work > Delphi XE2 does not ;) Delphi XE does not accept constants either, but scalar variables. So the following compiles:

Given the above, I suggest to leave Delphi mode as is, and forbid scalar values in FPC mode.

Alexander S. Klenin

Reply by email

Jonas Maebe
to Alexander Klenin

Alexander Klenin wrote on Mon, 11 Jun 2012:

[passing single elements as open array parameters]

Given the above, I suggest to leave Delphi mode as is, and forbid scalar values in FPC mode.

We can probably add a modeswitch that you can use to explicitly disable this type conversion.

Jonas

Reply by email