Sign in Join

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

Mailing lists / fpc-devel / 2019-02

Implicit cast from Char/String to array of Char

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

Ondrej Pokorny

Hello!

Implicit cast from Char/String to array of Char: is this a wanted behavior or is it a compiler bug?

(Btw. MODE DELPHI behaves the same).

program Project1;
{$mode objfpc}{$H+}
procedure A(AC: array of Char);
var
   C: Char;
begin
   for C in AC do
     WriteLn(C);
end;
procedure B(AC: array of Integer);
var
   C: Integer;
begin
   for C in AC do
     WriteLn(C);
end;
begin
   A('a');  // no error in FPC (error in Delphi)
   A('ab'); // no error in FPC (error in Delphi)
   B(1);    // error in FPC & Delphi
end.

Ondrej

Reply by email

Sven Barth
to Ondrej Pokorny
Ondrej Pokorny wrote 30 lines
Hello! Implicit cast from Char/String to array of Char: is this a wanted behavior or is it a compiler bug? (Btw. MODE DELPHI behaves the same). program Project1; {$mode objfpc}{$H+} procedure A(AC: array of Char); var   C: Char; begin   for C in AC do     WriteLn(C); end; procedure B(AC: array of Integer); var   C: Integer; begin   for C in AC do     WriteLn(C); end; begin   A('a');  // no error in FPC (error in Delphi)   A('ab'); // no error in FPC (error in Delphi)   B(1);    // error in FPC & Delphi end.

Please note that the error for "B(1)" in FPC is misleading. If you use "B(Integer(1))" it will work.
The reason is that both FPC and Delphi allow the passing of singular elements to open array parameters however Delphi does not allow constant values while FPC does.
Thus it's also clear why "A('a')" is working.
It's unclear however whether "A('ab')" is supposed to be working. I know that the compiler has some code to convert a string constant to char arrays, but I don't know if this is intended to be used here as well.

Regards,
Sven

Reply by email