[Pas2js] Question regarding the future type helper feature

Sven Barth pascaldragon at googlemail.com
Sat Jan 5 20:59:05 CET 2019


Am 05.01.2019 um 10:52 schrieb Michael Van Canneyt:
>
>
> On Sat, 5 Jan 2019, Sven Barth via Pas2js wrote:
>
>>>> Many JS plugins works extending prototype, and we can use multiple 
>>>> plugins
>>>> simultaneously, so it should be supported in Pas2JS mode. :-)
>>>
>>> I had thought of this myself already. If possible, we will do it.
>> Ryan had published a patch on one of the fpc mailing lists for FPC 
>> for this specific feature and I myself wanted that in FPC as well. So 
>> it's likely that this gets added to FPC as well - we should only make 
>> sure then that the same methods are called when there is an ambiguity.
>
> The last one in scope should be called, no ?
>
> As far as I can see this does mean you must have some restrictions: if 
> there are 2 type helpers for a type in one unit (something which I would
> forbid anyway), they cannot contain a method with the same signature.
>
> But if they are in 2 separate units, the one from the last unit in the 
> uses
> clause must be used, just as for regular procedures. The same rules 
> apply, I
> would think.
Please note that right now multiple helpers in the same unit are already 
allowed. It's just that only the last declared one is used (some of the 
tests in test\t*hlp*.pp check this).

All the methods of the suitable helpers in scope should be part of the 
available overloads with those of the same signature being picked by the 
scope order.

For example:

=== code begin ===

type
   TObjectHelper = class helper for TObject
     procedure Test1;
     procedure Test2(aArg: LongInt);
     procedure Test2(aArg: String);
   end;

   TObjectHelper2 = class helper for TObject
     procedure Test3;
     procedure Test2(aArg: Boolean);
     procedure Test2(aArg: String);
   end;

var
   o: TObject;
begin
   o.Test1; // will call TObjectHelper.Test1
   o.Test3; // will call TObjectHelper2.Test3
   o.Test2(42); // will call TObjectHelper.Test2(LongInt)
   o.Test2(True); // will call TObjectHelper2.Test2(Boolean)
   o.Test2('Hello World'); // will call TObjectHelper2.Test2(String)
end.

=== code end ===

Regards,
Sven


More information about the Pas2js mailing list