[fpc-pascal] optional parameter
denis.spir at gmail.com
Mon May 3 23:38:06 CEST 2010
On Mon, 03 May 2010 19:15:57 +0100
Martin <fpc at mfriebe.de> wrote:
> On 03/05/2010 10:58, spir ☣ wrote:
> > Hello,
> > A few questions on the topic:
> > * Is it possible to define an optional parameter without default value?
> > * Is it at all possible for an optional *argument* not to be the last in actual call? Eg
> > procedure p(a:Integer=0 ; b:Integer=0)
> > How can the caller pass b and not a?
> > * The ref states: "For dynamic arrays or other types that can be considered as equivalent to a pointer, the only possible default value is Nil."
> > Does this apply to any non-atomic type? Which are the "other types that can be considered as equivalent to a pointer"?
> > Tried with a record, but cannot make it be accepted by the compiler. Needed to change the parameter to be a pointer to the record instead (so the default can be nil), but this creates a trap for the calling code.
> > Else, is there a common trick or workaround?
> Maybe you want to use overloading instead of optioanl parameters?
> Procedure Foo; overload; // no params
> Procedure Foo(SomeRecord: TRecordType); overload; // no params
> then of course you need to implement twice
> Procedure Foo;
> var a: TRecordType;
> a:= xxx;/your defaults
> Procedure Foo(SomeRecord: TRecordType);
> / do work
Right, thank you, did not know "overload". This seems to be a good solution.
vit esse estrany ☣
More information about the fpc-pascal