Sign in Join

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

Mailing lists / fpc-pascal / 2012-03

Passing a string to open array of string

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

cobines

Hello.

The following program compiles and correctly prints 'something'. Is it something supported or a bug? When I change it to Proc('something') it doesn't compile (as expected).

program Project1;
{$mode objfpc}{$H+}
procedure Proc(const s: array of string);
begin
  WriteLn(s[0]);
end;
var
  s: string = 'something';
begin
  Proc(s);
end.
cobines
Jonas Maebe
to cobines
cobines wrote
The following program compiles and correctly prints 'something'. Is it something supported or a bug?

It is supported. Even Turbo Pascal already supported passing a single element as an open array parameter.

When I change it to Proc('something') it doesn't compile (as expected).

That's probably a bug, although constant strings are hard/special: a constant string can be an array of char, a shortstring, an ansistring, a widestring and if it's one character also a char or widechar.

Jonas

Martin
to Jonas Maebe
Jonas Maebe wrote 6 lines
On 31 Mar 2012, at 12:07, cobines wrote: > The following program compiles and correctly prints 'something'. Is it > something supported or a bug? It is supported. Even Turbo Pascal already supported passing a single element as an open array parameter.

Interesting, yet the following overloaded functions, compile too. In other cases, if more than one match for a call exists, it gives an error.

special optimization?

program Project1;
{$mode objfpc}{$H+}
procedure Proc(const s: array of string); overload;
begin
   WriteLn(s[0]);
end;
procedure Proc(const s: string); overload;
begin
   WriteLn(s);
end;
var
   s: string = 'something';
begin
   Proc(s);
   readln
end.
Jonas Maebe
to Martin
Martin wrote
On 31/03/2012 19:34, Jonas Maebe wrote: > It is supported. Even Turbo Pascal already supported passing a single element as an open array parameter. Interesting, yet the following overloaded functions, compile too. In other cases, if more than one match for a call exists, it gives an error.

...

procedure Proc(const s: array of string); overload; procedure Proc(const s: string); overload;

The compiler only gives an error if it cannot decide which of the two or more found overloads is is the best match. In this case, the best match is clear (the non-open-array version matches exactly, the open-array matches only after a quite convoluted type conversion). There are many cases where multiple potential matches exist and the compiler will not complain.

Jonas

cobines
to Jonas Maebe: It is supported. Even Turbo Pascal already supported passing a single element as an open…

2012/3/31 Jonas Maebe :

Show quoted text 7 lines
On 31 Mar 2012, at 12:07, cobines wrote: > The following program compiles and correctly prints 'something'. Is it > something supported or a bug? It is supported. Even Turbo Pascal already supported passing a single element as an open array parameter.

Thanks. I didn't found it in documentation, so I asked. I changed a function parameter from string to array of string and compiler showed error in places where function was called with const string but did not where variable was being passed. I just wanted to know if I needed to find and fix those places by hand. But if it works by design then I won't need to.

cobines