boggy
boggy

Reputation: 4027

Why does the compiler reject a constant default parameter value with "E2026 Constant expression expected"?

The following code fails to compile with [dcc32 Error] TestDefaultParameterValuePrj.dpr(13): E2026 Constant expression expected:

program TestDefaultParameterValuePrj;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils;

const
  SomeValue: char = 'T';

  function Test(p1: String; p2: char = SomeValue): String;
  begin
    Result := p2;
  end;

begin
  try
    Writeln(Test('Blah Blah'));
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Based on this thread I could use an overloaded function if I really wanted to use the constant. However, I am surprised the compiler doesn't accept it.

Upvotes: 4

Views: 1369

Answers (1)

David Heffernan
David Heffernan

Reputation: 612993

From the documentation, with my emphasis:

You can specify default parameter values in a procedure or function heading. Default values are allowed only for typed const and value parameters. To provide a default value, end the parameter declaration with the = symbol followed by a constant expression that is assignment-compatible with the parameter's type.

A typed constant is not a constant expression. Declare your constant as a true constant which is a constant expressioin:

const
  SomeValue = 'T';

The somewhat thorny issue of true constants, typed constants and constant expressions is covered here: Declared Constants. Some key excerpts:

A constant expression is an expression that the compiler can evaluate without executing the program in which it occurs. Constant expressions include numerals; character strings; true constants; values of enumerated types; the special constants True, False, and nil; and expressions built exclusively from these elements with operators, typecasts, and set constructors.

....

This definition of a constant expression is used in several places in Delphi's syntax specification. Constant expressions are required for initializing global variables, defining subrange types, assigning ordinalities to values in enumerated types, specifying default parameter values, writing case statements, and declaring both true and typed constants.

....

Typed constants, unlike true constants, can hold values of array, record, procedural, and pointer types. Typed constants cannot occur in constant expressions.

Upvotes: 5

Related Questions