Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2010-05

optional parameter

5 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

spir ☣

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?

Denis
________________________________

vit esse estrany ☣

spir.wikidot.com

Flávio Etrusco
to spir ☣

2010/5/3 spir ☣ :

Show quoted text 9 lines
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?

No and no.

* 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"?

Class, AnsiString.

Show quoted text 5 lines
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?

What trap?

-Flávio

Martin
to spir ☣: A few questions on the topic: * Is it possible to define an optional parameter without…
spir ☣ wrote 15 lines
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;
begin
    a:= xxx;/your defaults
   Foo(a);
end;
Procedure Foo(SomeRecord: TRecordType);
begin
   / do work
end;
spir ☣
to Martin
Show quoted text 35 lines
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; begin a:= xxx;/your defaults Foo(a); end; Procedure Foo(SomeRecord: TRecordType); begin / do work end;

Right, thank you, did not know "overload". This seems to be a good solution.

Denis
________________________________

vit esse estrany ☣

spir.wikidot.com

spir ☣
to Flávio Etrusco: 2010/5/3 spir ☣ : No and no. Class, AnsiString. What trap? -Flávio
Show quoted text 7 lines
> 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? > What trap?

Simply that the caller must know a pointer must be passed instead of the parameter. And think at it everytime.

Denis
________________________________

vit esse estrany ☣

spir.wikidot.com