On 30/12/10 7:40, David Emerson wrote:
I'd like to use a constant record as a default parameter. Is there some way to
do this? Here's my use case:

type
   lt_ints = record
     left, top : longint;
     end;

const
   lt_zero : lt_ints = (left:0; top:0);

procedure do_something (const offset_lt : lt_ints = lt_zero);


The procedure declaration gives an error ("illegal expression").

I assume the trouble is that lt_zero cannot be used as a default parameter
value, because it is not truly a constant: it is an initialized variable.

Perhaps my real question is: how do I make a constant record which is truly
constant, rather than an initialized variable?

AFAIK default parameters can only be simple types, not complex types like records. Although, I did get the following program using a default class parameter to compile and run. (And I thought you could not have var default parameters).

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, Forms, Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  end;

  Tduo = class
  public
    top, left: longint;
  end;

var  Form1: TForm1;

implementation

{$R *.lfm}

procedure TForm1.Button1Click(Sender: TObject);
var duo: Tduo=nil;

  procedure Doit(var du: TDuo = nil);
  begin
    if du = nil then
     begin
     du := TDuo.Create;
     du.top:=2;
     end;
  end;

begin
  Doit(duo);
  ShowMessageFmt('duo.top has value %d',[duo.top]);
  duo.Free;
end;

end.

Howard
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply via email to