Class helper properties

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Class helper properties

Ryan Joseph
http://wiki.freepascal.org/Helper_types#Usage says class helpers can use properties. Why am I getting this error?

program test;

type
        TMyObject = class
                m_num: integer;
                property num1: integer read m_num;
        end;
        THelper = class helper for TMyObject
                property num2: integer read m_num; // ERROR: Unknown class field or method identifier "m_num"
        end;

begin
end.


Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Class helper properties

Anthony Walter-3
Ryan, I am guessing in the declaration you need to read or write from methods on the helper only and not from fields or methods on the type you are extending.

type
  TMyObject = class
  private
    FNum: Integer;
  public
    property Num1: Integer read FNum;
  end;

  TMyObjectHelper = class helper for TMyObject
  private
    function GetNum2: Integer;
  public
    property Num2: Integer read GetNum2;
  end;

implementation

function TMyObjectHelper,GetNum2: Integer;
begin
  Result := FNum;
end;

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Class helper properties

Free Pascal - General mailing list
In reply to this post by Ryan Joseph
On Sun, 24 Feb 2019 11:03:17 -0500
Ryan Joseph <[hidden email]> wrote:

> http://wiki.freepascal.org/Helper_types#Usage says class helpers can
> use properties. Why am I getting this error?
>
> program test;
>
> type
> TMyObject = class
> m_num: integer;
> property num1: integer read m_num;
> end;
> THelper = class helper for TMyObject
> property num2: integer read m_num; // ERROR:
> Unknown class field or method identifier "m_num" end;

This is Delphi compatible.
A helper property can only access helper methods.
A property of a class can access helper methods.

pas2js allows both directions.

Mattias
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal