[fpc-devel] Functors

Michael Van Canneyt michael at freepascal.org
Sun Dec 26 22:03:28 CET 2021



On Sun, 26 Dec 2021, Blaise--- via fpc-devel wrote:

> On 26.12.2021 19:33, Michael Van Canneyt via fpc-devel wrote:
>> On Sun, 26 Dec 2021, Blaise--- via fpc-devel wrote:
>>> On 26.12.2021 11:50, Michael Van Canneyt via fpc-devel wrote:
>>>> None of what is shown below cannot be handled by ordinary methods
>>>
>>> Well, yes. But, following your reasoning, the same should be said about 
> overloading most of the operators: "why is aC+aD better than aC.Add(aD)"?
>> 
>> No. Operators are clearly expressions. ac.Add(aD) is a statement.
>
> How can you tell just from that? :) If Add is a function (which is clearly 
> implied by the comparison with the operator Add), then "aC.Add(aD)" is an 
> expression, just like "aC+aD".

Well: you write it: IF.
With aC+aD there is no IF, it's an expression. Clear.

>
> Let me offer another comparison and rephrase my quote: following your 
> reasoning, the same should be said about default array properties: "why is 
> aC[42] better than aC.Items[42]"?

I would not have introduced this. Delphi has.

>
> with the modern Object Pascal as implemented by DCC and FPC?
>
>> I would reject your proposal using Invoke.
>
> Let us be precise here. You mean "procedure/function Invoke", right? Because 
> "operator Invoke" (which is part of my proposal) would not clash with other 
> entities named Invoke.

procedure/function invoke. How the operator is called, I honestly don't care,
although I would be more in favour of call.


>
>> Reusing "default"
>
> As I wrote in my response to Ryan, I actually quite like that, except for one 
> reservation: should the support for default interface methods be ever added 
> to FPC, the DEFAULT directive would fit there better, IMO.
> A dedicated directive would be fine with me, and it could be used to 
> designate any compatible method as any operator:
> -------8<-------
> type R = record
> 	procedure Foo(...); operator ();
> 	function Bar(const A, B: R): R; operator +;
> end;
> -------8<-------
>
> How about another approach, albeit more complex: does FPC support method 
> aliasing? Personally, many times, I wished DCC had that.
> -------8<-------
> type R = record
> 	procedure Foo(...);
> 	operator () = Foo; // routine alias
> 	// OR: operator Invoke = Foo;
> end;
> -------8<-------
> Routine aliasing is a more general (hence, more useful) feature on its own; 
> and with it, even inherited methods can be designated as operators in derived 
> classes.

I think you're getting carried away a little ;-)

Let's stick to the topic at hand:

I am a fan of KISS principle. Hence reusing default, because it is already a
keyword. For every additional keyword, we need to change umpteen routines in
various places. So let's keep that to a minimum.

So I repeat my point of view: Adding the feature is OK, but let's try to keep
impact minimal. For me this means, reuse 'default' keyword and add an operator.
What the name is (call, invoke) does not matter for me...

Michael.


More information about the fpc-devel mailing list