Sign in Join

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

Mailing lists / fpc-pascal / 2012-06

Default value for an open array

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

Mark Morgan Lloyd

It's previously been pointed out to me that a declaration such as

     procedure OutputWriteF(const str: widestring; values: array of
const; fg: TColor= clBlack; bg: TColor= clDefault);

can't be rewritten

type owfArray: array of const;
     procedure OutputWriteF(const str: widestring; values: owfArray; fg:
TColor= clBlack; bg: TColor= clDefault);

since this would make the parameter into a dynamic rather than an open array. I'm entirely happy to accept that now that I understand it.

However, given a declaration of that form, is it possible to define a default parameter of [] so that OutputWriteF('Test, no params\n') is valid?

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]
kyan
to Mark Morgan Lloyd
However, given a declaration of that form, is it possible to define a default parameter of [] so that OutputWriteF('Test, no params\n') is valid?

Not for open arrays, but you can write an overloaded version without the open array argument that calls the version with the open array parameter passing an empty array, or an array initialised with whatever "default" values you want:

interface
procedure OutputWriteF(const str: widestring); overload;
procedure OutputWriteF(const str: widestring; values: array of const); overload;
implementation
procedure OutputWriteF(const str: widestring);
begin
  OutputWriteF(str, [clBlack, clDefault]);
end;
procedure OutputWriteF(const str: widestring; values: array of const);
begin
 ...
end;

HTH

Constantine.

Mark Morgan Lloyd
to kyan
kyan wrote 8 lines
> However, given a declaration of that form, is it possible to define a > default parameter of [] so that OutputWriteF('Test, no params\n') is > valid? Not for open arrays, but you can write an overloaded version without the open array argument that calls the version with the open array parameter passing an empty array, or an array initialised with whatever "default" values you want:

Thanks, good point and adequate workaround.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]