[fpc-pascal] constant records as default parameters
hdpc at talktalk.net
Fri Dec 31 00:28:58 CET 2010
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:
> lt_ints = record
> left, top : longint;
> 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).
Classes, Forms, Dialogs, StdCtrls;
TForm1 = class(TForm)
procedure Button1Click(Sender: TObject);
Tduo = class
top, left: longint;
var Form1: TForm1;
procedure TForm1.Button1Click(Sender: TObject);
var duo: Tduo=nil;
procedure Doit(var du: TDuo = nil);
if du = nil then
du := TDuo.Create;
ShowMessageFmt('duo.top has value %d',[duo.top]);
More information about the fpc-pascal