Sign in Join

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

Mailing lists / fpc-pascal / 2013-09

function param and TVarRec

4 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

Xiangrong Fang

Hi There,

If you declare this:

procedure proc(param: array of const);
Then you can pass any type of varaible to param, however, only in an array,
such as proc([1, 2]);

Is it possible to achieve the effect of the code below (which is wrong):

procedure proc(p1: const; p2: const);

So that: 1) p1 and p2 is translated by the compiler to TVarRec inside the function; 2) I do not need to pass an array to the function, in another word, the number of params for the procedure is *fixed*, but the *type* of each param is not.

Regards,
Xiangrong

Reply by email

Michael Van Canneyt
to Xiangrong Fang
Xiangrong Fang wrote 14 lines
Hi There, If you declare this: procedure proc(param: array of const); Then you can pass any type of varaible to param, however, only in an array, such as proc([1, 2]); Is it possible to achieve the effect of the code below (which is wrong): procedure proc(p1: const; p2: const); So that: 1) p1 and p2 is translated by the compiler to TVarRec inside the function; 2) I do not need to pass an array to the function, in another word, the number of params for the procedure is *fixed*, but the *type* of each param is not.

Use variants:

Procedure Proc (Var A,B : variant);

Michael.

Reply by email

Xiangrong Fang
to Michael Van Canneyt

Thanks, this works, sort of. But does not meet my needs, I will pass in either an int value or an object, but Variant is not compatible with TObject.

Alternatively, this also does NOT work: array [0..1] of const; no matter it is a type definition or used as function parameter.

This is a trivial problem, I can just go with array of const, I am just curious if pascal can do this, because it will make the function definition more strict and my code cleaner.

Regards,
Xiangrong

2013/9/24 Michael Van Canneyt

Show quoted text 33 lines
On Tue, 24 Sep 2013, Xiangrong Fang wrote: Hi There, > > If you declare this: > > procedure proc(param: array of const); > > Then you can pass any type of varaible to param, however, only in an > array, such as proc([1, 2]); > > Is it possible to achieve the effect of the code below (which is wrong): > > procedure proc(p1: const; p2: const); > > So that: 1) p1 and p2 is translated by the compiler to TVarRec inside the > function; 2) I do not need to pass an array to the function, in another > word, the number of params > for the procedure is *fixed*, but the *type* of each param is not. > Use variants: Procedure Proc (Var A,B : variant); Michael. ______________________________**_________________ fpc-pascal maillist - http://lists.freepascal.org/**mailman/listinfo/fpc-pascal<http://lists.freepascal.org/mailman/listinfo/fpc-pascal>

Reply by email

Michael Van Canneyt
to Xiangrong Fang
Xiangrong Fang wrote
Thanks,  this works, sort of.  But does not meet my needs, I will pass in either an int value or an object, but Variant is not compatible with TObject.

You could make it so using a custom variant manager or operator overloading, but this requires additional work.

Michael.

Reply by email