[fpc-devel] Functors

Blaise at blaise.ru Blaise at blaise.ru
Sun Dec 26 19:44:05 CET 2021

On 26.12.2021 17:40, Ryan Joseph via fpc-devel wrote:
> I'm 99% certain using the method name "Invoke" would be rejected on the grounds of backwards compatibility.

Not to argue for the "procedure/function Invoke" syntax, but it hardly breaks backward compatibility. Only in the purest non-practical sense: "I maintain this code that always used to be rejected and suddenly it gets compiled".

> Adding a new syntax using "default" like "function Add9(const N: Integer): Integer; default;"

That is pretty much the /old/ syntax.
I agree with Michael that it would mirror default properties nicely, but:
1) Are we most likely to name that "default method" or "call operator"?
2) We now have proper operator overloading; I would even argue that, were it added to Object Pascal much earlier, we would have had "operator Index" instead of default array properties;
3) I would rather reserve the DEFAULT directive on methods for default interface methods:
type I = interface
	procedure Foo; default;
	// the above, IMO, looks better than
	//  procedure Foo; not abstract;
procedure I.Foo; begin { default implementation } end;

> 	class operator Call(....);

Again, the proposal is:
type R = record
	operator Invoke(...)...;
A) without CLASS:
	1) Having to declare Self manually would be ridiculous;
	2) In interfaces, that method should be instance-virtual.
B) The name Invoke is required for compatibility with DCC.

Granted, for "operator", some hackery would still be required for 100% DCC compatibility:
type MethRef = reference to procedure;
type C = class(TInterfacedObject, MethRef)
	// This needs to map to _operator_ MethRef.Invoke
	procedure Invoke;

> it doesn't present any new syntax

So, in your book, introducing a /new/ operator identifier "doesn't present any new syntax" (and I agree), but semantically allowing the /existing/ directive DEFAULT to appear in the existing list of method directives is somehow "a new syntax". You have some interesting definition of "syntax".


More information about the fpc-devel mailing list