Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2019-04

Record helper properties

13 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

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

Reply by email

Ryan Joseph
to 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?

Show quoted text 18 lines
On Apr 20, 2019, at 11:24 AM, Ryan Joseph 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

Reply by email

Michael Van Canneyt
to Ryan Joseph
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.

Reply by email

Ryan Joseph
to Michael Van Canneyt
Show quoted text 8 lines
On Apr 23, 2019, at 5:32 PM, Michael Van Canneyt 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

Reply by email

Sven Barth
to Ryan Joseph: So I remember now that any fields are not allowed in helpers. Can anyone explain why this…

Ryan Joseph 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

Reply by email

Ryan Joseph
to Sven Barth
Show quoted text 5 lines
On Apr 23, 2019, at 6:25 PM, Sven Barth via fpc-pascal 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

Reply by email

Ryan Joseph
to Alexander Grotewohl
Show quoted text 8 lines
On Apr 23, 2019, at 8:28 PM, Alexander Grotewohl 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

Reply by email

Tomas Hajny
to Ryan Joseph
Ryan Joseph wrote 55 lines
> On Apr 23, 2019, at 8:28 PM, Alexander Grotewohl > 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 - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email

Michael Van Canneyt
to Ryan Joseph: I mean for properties though so it’s not actually adding a type, just making an alias:…
Ryan Joseph wrote 16 lines
> On Apr 23, 2019, at 5:32 PM, Michael Van Canneyt 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.

Reply by email

Ryan Joseph
to Michael Van Canneyt
On Apr 24, 2019, at 2:27 AM, Michael Van Canneyt 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

Reply by email

Sven Barth
to Ryan Joseph
Ryan Joseph wrote 5 lines
> On Apr 24, 2019, at 2:27 AM, Michael Van Canneyt 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

Reply by email

Mattias Gaertner
to Sven Barth
Show quoted text 16 lines
Am 24.04.2019 um 15:28 schrieb Ryan Joseph: > >> On Apr 24, 2019, at 2:27 AM, Michael Van Canneyt >> 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

Reply by email