Record helper properties

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

Record helper properties

Ryan Joseph
I feel like I may have asked this before but I can’t find anything on Google. Why doesn’t this work?

program test;

type
        TRec = record
                x: integer;
        end;

type
        THelper = record helper for TRec
                property F: integer read x; // <<<—— ERROR: Unknown class field or method identifier "x"
        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: Record helper properties

Ryan Joseph
So I remember now that any fields are not allowed in helpers. Can anyone explain why this must be the case or if it could be fixed?

> On Apr 20, 2019, at 11:24 AM, Ryan Joseph <[hidden email]> wrote:
>
> I feel like I may have asked this before but I can’t find anything on Google. Why doesn’t this work?
>
> program test;
>
> type
> TRec = record
> x: integer;
> end;
>
> type
> THelper = record helper for TRec
> property F: integer read x; // <<<—— ERROR: Unknown class field or method identifier "x"
> 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: Record helper properties

Michael Van Canneyt


On Tue, 23 Apr 2019, Ryan Joseph wrote:

> So I remember now that any fields are not allowed in helpers. Can anyone explain why this must be the case or if it could be fixed?

That should be obvious:

Because a helper can only add methods to a type, not additional storage
space (memory). The basic type determines the memory layout of the data.
A helper cannot change that.

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

Re: Record helper properties

Ryan Joseph


> On Apr 23, 2019, at 5:32 PM, Michael Van Canneyt <[hidden email]> wrote:
>
> That should be obvious:
>
> Because a helper can only add methods to a type, not additional storage
> space (memory). The basic type determines the memory layout of the data. A helper cannot change that.
>
> Michael.

I mean for properties though so it’s not actually adding a type, just making an alias:

property F: integer read x;

that should just map “x” to “F”, right? I’m getting an error on this however.

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: Record helper properties

Free Pascal - General mailing list
In reply to this post by Ryan Joseph
Ryan Joseph <[hidden email]> schrieb am Di., 23. Apr. 2019, 21:54:
So I remember now that any fields are not allowed in helpers. Can anyone explain why this must be the case or if it could be fixed?

A helper can not access its extended type in its interface, only in its implementation (and yes, this gets important if published properties are involved, which a helper may declare as well).
Use a getter as a workaround. If you use "inline" chances are high that it will be nearly equal code as if you used a direct access. 

Regards, 
Sven 

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

Re: Record helper properties

Ryan Joseph


> On Apr 23, 2019, at 6:25 PM, Sven Barth via fpc-pascal <[hidden email]> wrote:
>
> A helper can not access its extended type in its interface, only in its implementation (and yes, this gets important if published properties are involved, which a helper may declare as well).
> Use a getter as a workaround. If you use "inline" chances are high that it will be nearly equal code as if you used a direct access.
>

Still getting errors. What did I do wrong?

{$mode objfpc}
{$modeswitch advancedrecords}

program test;

type
  TRec = record
    x: integer;
    function GetX: integer;
    procedure SetX (newValue: integer);
  end;

function TRec.GetX: integer;
begin
  result := x;  
end;

procedure TRec.SetX (newValue: integer);
begin
  x := newValue;
end;

type
  THelper = record helper for TRec
    property F: integer read GetX write SetX; // ERROR: Unknown class field or method identifier "GetX"
  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: Record helper properties

Alexander Grotewohl
http://wiki.freepascal.org/Helper_types

under the checklistbox extender example.

I'd guess it's looking for THelper.GetX and THelper.SetX in your  and not finding them

--
Alexander Grotewohl
http://dcclost.com

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

Re: Record helper properties

Ryan Joseph


> On Apr 23, 2019, at 8:28 PM, Alexander Grotewohl <[hidden email]> wrote:
>
> http://wiki.freepascal.org/Helper_types
>
> under the checklistbox extender example.
>
> I'd guess it's looking for THelper.GetX and THelper.SetX in your  and not finding them
>

Oh now I get it. Is this what I should be doing? I really didn’t want those getters so I’m putting them as strict private. The error message could be better on this.

Honestly this seems kind of crazy though. We’re making all this boiler plate because published fields? Why not just allow field access unless there are published fields? I’ve never used published fields personally so it’s an unfortunate restriction.

program test;

type
  TRec = record
    x: integer;
  end;

type
  THelper = record helper for TRec
    strict private
      function GetX: integer; inline;
      procedure SetX (newValue: integer); inline;
    public
      property F: integer read GetX write SetX;
  end;

function THelper.GetX: integer;
begin
  result := x;  
end;

procedure THelper.SetX (newValue: integer);
begin
  x := newValue;
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: Record helper properties

Tomas Hajny-2
On 2019-04-24 02:36, Ryan Joseph wrote:

>> On Apr 23, 2019, at 8:28 PM, Alexander Grotewohl <[hidden email]>
>> wrote:
>>
>> http://wiki.freepascal.org/Helper_types
>>
>> under the checklistbox extender example.
>>
>> I'd guess it's looking for THelper.GetX and THelper.SetX in your  and
>> not finding them
>>
>
> Oh now I get it. Is this what I should be doing? I really didn’t want
> those getters so I’m putting them as strict private. The error message
> could be better on this.
>
> Honestly this seems kind of crazy though. We’re making all this boiler
> plate because published fields? Why not just allow field access unless
> there are published fields? I’ve never used published fields
> personally so it’s an unfortunate restriction.
>
> program test;
>
> type
>   TRec = record
>     x: integer;
>   end;
>
> type
>   THelper = record helper for TRec
>     strict private
>       function GetX: integer; inline;
>       procedure SetX (newValue: integer); inline;
>     public
>       property F: integer read GetX write SetX;
>   end;
>
> function THelper.GetX: integer;
> begin
>   result := x;
> end;
>
> procedure THelper.SetX (newValue: integer);
> begin
>   x := newValue;
> end;
>
> begin
> end.
>
> Regards,
> Ryan Joseph
>
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Record helper properties

Michael Van Canneyt
In reply to this post by Ryan Joseph


On Tue, 23 Apr 2019, Ryan Joseph wrote:

>
>
>> On Apr 23, 2019, at 5:32 PM, Michael Van Canneyt <[hidden email]> wrote:
>>
>> That should be obvious:
>>
>> Because a helper can only add methods to a type, not additional storage
>> space (memory). The basic type determines the memory layout of the data. A helper cannot change that.
>>
>> Michael.
>
> I mean for properties though so it’s not actually adding a type, just making an alias:
>
> property F: integer read x;
>
> that should just map “x” to “F”, right? I’m getting an error on this however.
I would think this should be allowed, yes. I see no reason to disallow it.

That said, I see also no reason why on earth you would want to add this, as you can
just access x ? The only sensible addition would be using a getter/setter,
to do some kind of check or transformation.

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

Re: Record helper properties

Ryan Joseph


> On Apr 24, 2019, at 2:27 AM, Michael Van Canneyt <[hidden email]> wrote:
>
> I would think this should be allowed, yes. I see no reason to disallow it.

Agreed but Sven says something about published fields though. The property just acts as simple alias so I don’t understand what the problem could be either. I’ll put this on my to-do list to fix in the compiler if it’s permitted.

>
> That said, I see also no reason why on earth you would want to add this, as you can
> just access x ? The only sensible addition would be using a getter/setter,
> to do some kind of check or transformation.

I just wanted to add some R,G,B fields to a vector instead of adding them into the vector itself. Minor code hygiene thing that would be nice to accommodate but all the accessors required now makes this not so appealing.

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: Record helper properties

Free Pascal - General mailing list
Am 24.04.2019 um 15:28 schrieb Ryan Joseph:
>
>> On Apr 24, 2019, at 2:27 AM, Michael Van Canneyt <[hidden email]> wrote:
>>
>> I would think this should be allowed, yes. I see no reason to disallow it.
> Agreed but Sven says something about published fields though. The property just acts as simple alias so I don’t understand what the problem could be either. I’ll put this on my to-do list to fix in the compiler if it’s permitted.
No, you're not permitted. The issues that would currently appear with
published properties will also appear with non-published ones once
support for extended properties is added. And I definitely don't want to
fiddle around with that only because someone didn't like to write some
boilerplate code.

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

Re: Record helper properties

Free Pascal - General mailing list
On Wed, 24 Apr 2019 17:16:05 +0200
Sven Barth via fpc-pascal <[hidden email]> wrote:

> Am 24.04.2019 um 15:28 schrieb Ryan Joseph:
> >  
> >> On Apr 24, 2019, at 2:27 AM, Michael Van Canneyt
> >> <[hidden email]> wrote:
> >>
> >> I would think this should be allowed, yes. I see no reason to
> >> disallow it.  
> > Agreed but Sven says something about published fields though. The
> > property just acts as simple alias so I don’t understand what the
> > problem could be either. I’ll put this on my to-do list to fix in
> > the compiler if it’s permitted.  
> No, you're not permitted. The issues that would currently appear with
> published properties will also appear with non-published ones once
> support for extended properties is added. And I definitely don't want
> to fiddle around with that only because someone didn't like to write
> some boilerplate code.

Can you give some details about the issues?

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