[fpc-devel] Function variables and overload

Bram Kuijvenhoven kuifwaremailinglists at xs4all.nl
Sun Jul 3 14:03:36 CEST 2005

Peter Vreman wrote:
> At 22:45 1-7-2005, you wrote:
>> function MyOverloadedFunc:boolean; alias MyOverLoadedFuncNoParams;
>> function MyOverloadedFunc(i:integer):boolean; alias MyOverLoadedFuncInt;
> You can already do this, only with some extra typing:
> function MyOverloadedFunc:boolean; public name 'MyOverLoadedFuncNoParams';
> function MyOverloadedFunc(i:integer):boolean; public name 
> 'MyOverLoadedFuncInt';
> function MyOverLoadedFuncNoParams:boolean; external name 
> 'MyOverLoadedFuncNoParams';
> function MyOverLoadedFuncInt(i:integer):boolean; external name 
> 'MyOverLoadedFuncInt';

This trick indeed works :)

Considering again why Marcel Martin actually asked for a feature to distinguish overloaded functions, maybe the aliases have limited use.

The function ICmp is mostly used in the context of passing it to sort functions or perhaps a container like a redblack tree or a heap (which need to know how to sort the items they store). In all these cases, it might be required to identify which ICmp function to use. A syntaxis like @ICmp:integer:integer or @ICmp(integer,integer) is more typing that ICmpII or some other short 'alias'. The only difference is that for ICmpII one needs to explictly declare the alias (using the trick you mentioned).

If ICmp was also called directly many times, then having the short form ICmp(myvar,myothervar) is easier to use than ICmpII(myvar,myothervar). In this case, the alias is useful.

A larger improvement would be reached by examining the context where the @ICmp is assigned to, e.g. the parameter type of the function you are passing @ICmp to, or perhaps the type of MyCmpProc in MyCmpProc:=@ICmp. This however can be ambiguous in case the you are passing @ICmp to an overloaded function (as I mentioned shortly before). In that case the programmer might need to explicitly identify which overloaded function of ICmp to pass. Anyway, the feature of  deriving the meaning of @ICmp from the context could save more typing and ease changing the type of variables more than when using only aliases (using the public name/external name trick or a new feature).

However, the 'context' feature sounds like 'not easy to implement' and 'dangerous because of unclear side effects' to me. So what is 'possible', as Marcel Martin said: When I ask 'would it be possible', I mean 'would it be possible
without having to rewite all the compiler'?

Personally I think maybe other features have more priority at the moment - I hope to see generics one day (these definitely help a lot in the area of type-safe containers) - but of course that is up to the developers to decide :)



More information about the fpc-devel mailing list