[fpc-devel] modeswitch multihelpers

Ryan Joseph ryan at thealchemistguild.com
Mon May 13 16:17:20 CEST 2019

Sorry I sent the first message to privately but I’m posting this to the list.

> On May 13, 2019, at 9:44 AM, Stefan Glienke <sglienke at dsharp.org> wrote:
> Now the author of THelper2 decides that it would be cool if to add a DoThis(msg: string); method.
> Now what happens to anyone calling DoThis from THelper1? Well if the same rules as with regular methods are applied here then the new DoThis method (not marked as overload) would hide the other one and it becomes uncallable. Yes, you can solve this by still looking if that method does not match - but what happens if multiple helpers add a bunch of DoThis overloads with different parameter types and signatures? There could be a match in the first found helper but actually a better match in a helper that came before (and it might even be that that one was called originally but not anymore now as someone introduced a matching one in another helper).
> C# extension methods are all treated the same and all as overload not causing any different behavior depending on the order they appear in the using.
> If you have the case where some call is ambiguous you have to call them as static method (see https://stackoverflow.com/a/8474979/587106).

I see what you mean now (in the example below you can just add “overload” to fix it of course). 

It sounds like you want something like "C# style function overloading” as a general feature of the language instead of the “overload” keyword and order of precedence rules that Pascal currently uses. The same problems you’re concerned about could happen with plain functions in different units so I don’t consider this a strictly helper related issue.

Personally I don’t like that approach because it means if an existing unit has a helper (or function) with the same signature the compiler will simply not allow you to overload it. Maybe you could argue for an additional function modifier that says “overload if not ambiguous” so you can be sure adding a new method doesn’t cover some existing method in the same scope.

{$mode objfpc}
{$modeswitch multihelpers}

program test;

  THelper1 = class helper for TObject
    procedure DoThis;

  THelper2 = class helper for TObject
    procedure DoThis(msg: string);

procedure THelper1.DoThis;

procedure THelper2.DoThis(msg: string);
  writeln('DoThis:', msg);

  obj: TObject;
  obj := TObject.Create;
  obj.DoThis;	// <======== ERROR: Wrong number of parameters specified for call to "DoThis"

	Ryan Joseph

More information about the fpc-devel mailing list