[fpc-devel] Function variables and overload

Marcel Martin mm10 at ellipsa.net
Sat Jul 2 01:56:07 CEST 2005


Peter Vreman a écrit :
>>Hello,
>>
>>Let's say there is a function
>>
>>  function ICmp(A,B: PBigInt): SInt32;
>>
>>and a TBigIntList class having the following method that can
>>sort the list according to Cmp
>>
>>  procedure TBigIntList.Sort(Cmp: TBigIntCompareFunc);
>>
>>with, of course,
>>
>>  type TBigIntCompareFunc = function(A,B: PBigInt): SInt32;
>>
>>
>>Now, if ICmp is not overloaded, there is no problem, the call
>>
>>  L.Sort(@ICmp);
>>
>>works. But, of course, it doesn't work if ICmp is overloaded.
>>In fact, we cannot have both overloaded functions _and_ the
>>the possibility to use them as variables. Yes, there are
>>ways to fix the problem (no overload or embedding the
>>overloaded functions in local functions) but none is really
>>satisfying.
>>
>>Would it be possible to get something like
>>
>>  L.Sort(@ICmp(PBigInt,PBigInt));
>>
>>in case ICmp is overloaded? If we could add the parameter
>>types, there would be no more ambiguity, the compiler would
> 
> 
> There is ambiguity with a normal call to the function. The compiler sees
> the ( and thinks it is parsing a call to Icmp().

Yes. I already encountered this problem when using a function
variable.
Ok. And this way

    L.Sort(@ICmp:PBigInt:PBigInt);

and if ever ICmp was overloaded but had no parameter

    L.Sort(@ICmp);

Well, of course, I don't know the compiler enough to propose
something clean and not ambiguous but the problem is not to find
a way to tell the compiler "Use this routine and not an other
one", I am sure it is not too much difficult. No, the problem is
"Is it easy to modify the parser in order to implement it?".

It would really be nice to be able to use overloaded routines as
non overloaded ones. At the moment, in order not to lose the
possibility to transmit routines to lists, like

   procedure TBigIntList.ForEach(Proc: TBigIntBinaryOperator;
     A: PBigInt; Test: TBigIntTestFunc=nil);

   // add A to all even integers of the list
   L.ForEach(@IAdd,A, at IIsEven);

   which is much faster than

   for i := 0 to L.Count-1 do
     if IIsEven(L[i]) then IAdd(L[i],A);

I suppressed most overloaded routines by adding suffixes: IAddUI32,
IAddSI32, IAddUI64, etc. It works but that's rather ugly :-)


To conclude, if you cannot add such a feature to FPC, you cannot.
Don't worry, FPC is already very good as it is.

mm




More information about the fpc-devel mailing list