[fpc-pascal] constant records as default parameters

David Emerson dle3ab at angelbase.com
Fri Dec 31 09:29:55 CET 2010


> AFAIK default parameters can only be simple types, not complex types 
> like records.

Well... it certainly appears to be so...

> Although, I did get the following program using a default  
> class parameter to compile and run.

Kinda defeats the purpose (low overhead). I'll stick with two simple params with 
defaults. (The other way would be to write more overloaded functions, but 
that's cumbersome, and has overhead of its own)

You know, it's just occurring to me that I wouldn't be saving anything anyway: 
any parameter with a default value must be passed-by-value, not by reference. 
So I'm passing two longints either way. Whether they're adjacent doesn't really 
make any difference at all.

> (And I thought you could not have var default parameters).

You can't. Here is a much shorter example:

{$mode objfpc}

procedure s (var a : longint = 0);
  begin
    inc (a);
  end;

begin
  s;  // Error: Variable identifier expected
end.

Perhaps it's a compiler bug that it doesn't complain about the indication of a 
var default parameter; however it definitely complains when you actually try to 
put that default parameter to use. I guess the default parameter part in the 
function header is simply being ignored.

Thanks for your input, though! I've got a more definitive answer now.

Although I still continue to wonder about constant records and record default 
params, even if they can't pass-by-ref.

~David.




More information about the fpc-pascal mailing list