[Pas2js] My 1st type helper

Sven Barth pascaldragon at googlemail.com
Sun Mar 3 11:52:07 CET 2019


Am 02.03.2019 um 20:09 schrieb Mattias Gaertner via Pas2js:
> On Sat, 2 Mar 2019 09:06:12 -0700 (MST)
> warleyalex via Pas2js <pas2js at lists.freepascal.org> wrote:
>
>> I think there's an issue: "class helper takes precedence over the
>> actual class type", for instance:
>> ----------------------------------
>> type
>>    TMyClass = class
>>      procedure MyProc;
>>      function MyFunc: Integer;
>>    end;
>>
>> type
>>    TMyClassHelper = class helper for TMyClass
>>      procedure HelloWorld;
>>      function MyFunc: Integer;
>>    end;
>>
>> implementation
>>
>> procedure TMyClass.MyProc;
>> var
>>   X: Integer;
>> begin
>>    X := MyFunc;
>> end;
>>
>> function TMyClass.MyFunc: Integer;
>> begin
>>    WriteLn('TMyClass.MyFunc called');
>> end;
>>
>> procedure TMyClassHelper.HelloWorld;
>> begin
>>    Writeln(Self.ClassName); // Self refers to TMyClass type, not
>> TMyClassHelper
>> end;
>>
>> function TMyClassHelper.MyFunc: Integer;
>> begin
>>    WriteLn('TMyClassHelper.MyFunc called');
>> end;
>>
>> (...)
>>
>> var
>>    X: TMyClass;
>> begin
>>    X := TMyClass.Create;
>>    X.MyProc;     // Calls TMyClass.MyProc
>>   // X.HelloWorld; // Calls TMyClassHelper.HelloWorld
>>    X.MyFunc;     // Calls TMyClassHelper.MyFunc
>> ----------------------------------
>> should output this:
>> (*
>>    TMyClass.MyFunc called
> No, helpers are active even inside the helped class itself.
Though there the search order is slightly different, if I remember 
correctly (especially when inherited helpers and/or the parents of the 
extended class are involved).

Regards,
Sven


More information about the Pas2js mailing list