Sign in Join

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

Mailing lists / fpc-pascal / 2012-01

Class helpers and properties

5 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

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

Mark Morgan Lloyd

If a class has a public reader property like this:

         TLexemeList= class(TObject)
         protected
           fValue: TPrimaevalValue;
         public
           property Value: TPrimaevalValue read fValue;
         ...

is there a way of allowing a class helper to augment this with a protected writer property? My attempt as below messed up the reader:

         TLLEvaluator = class helper for TLexemeList
         private
           procedure SetValue(v: TPrimaevalValue);
         protected
           property Value: TPrimaevalValue write SetValue;
         ...
Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Sven Barth
to Mark Morgan Lloyd
Mark Morgan Lloyd wrote 19 lines
If a class has a public reader property like this: TLexemeList= class(TObject) protected fValue: TPrimaevalValue; public property Value: TPrimaevalValue read fValue; ... is there a way of allowing a class helper to augment this with a protected writer property? My attempt as below messed up the reader: TLLEvaluator = class helper for TLexemeList private procedure SetValue(v: TPrimaevalValue); protected property Value: TPrimaevalValue write SetValue; ...

In your exact example fValue is protected so the following should work:

property Value: TPrimaevalValue read fValue write SetValue;

Another possibility (for non protected properties):

property Value: TPrimaevalValue read GetValue write SetValue;

...

function TLLEvaluator.GetValue: TPrimaevalValue;
begin
   Result := inherited Value;
end;

Other possibilities don't exist, as a helper's declaration will hide the declaration of the extended class (exceptions are methods with "overload" defined).

Regards,
Sven

Reply by email

Mark Morgan Lloyd
to Sven Barth
Sven Barth wrote 9 lines
On 27.01.2012 22:46, Mark Morgan Lloyd wrote: > If a class has a public reader property like this: > > TLexemeList= class(TObject) > protected > fValue: TPrimaevalValue; > public > property Value: TPrimaevalValue read fValue; > ...

Note that below is in a different unit.

Show quoted text 14 lines
> is there a way of allowing a class helper to augment this with a > protected writer property? My attempt as below messed up the reader: > > TLLEvaluator = class helper for TLexemeList > private > procedure SetValue(v: TPrimaevalValue); > protected > property Value: TPrimaevalValue write SetValue; > ... > In your exact example fValue is protected so the following should work: property Value: TPrimaevalValue read fValue write SetValue;

That didn't work since once we're in the helper it can't see fValue since it's only visible in the original unit.

Obviously code in the helper can simply call SetValue(), but I thought it worth raising in case I was missing something obvious.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Sven Barth
to Mark Morgan Lloyd
Mark Morgan Lloyd wrote 31 lines
Sven Barth wrote: > On 27.01.2012 22:46, Mark Morgan Lloyd wrote: >> If a class has a public reader property like this: >> >> TLexemeList= class(TObject) >> protected >> fValue: TPrimaevalValue; >> public >> property Value: TPrimaevalValue read fValue; >> ... Note that below is in a different unit. >> is there a way of allowing a class helper to augment this with a >> protected writer property? My attempt as below messed up the reader: >> >> TLLEvaluator = class helper for TLexemeList >> private >> procedure SetValue(v: TPrimaevalValue); >> protected >> property Value: TPrimaevalValue write SetValue; >> ... >> > > In your exact example fValue is protected so the following should work: > > property Value: TPrimaevalValue read fValue write SetValue; That didn't work since once we're in the helper it can't see fValue since it's only visible in the original unit.

You are right that it does not work, but you're not right about the reason. Helpers are able to access published, public and protected members of its extended class (e.g. you're able to write "fValue := aValue" in your setter inside the helper), but you can't make use of fields and methods of the extended class in the helper's declaration.

Obviously code in the helper can simply call SetValue(), but I thought it worth raising in case I was missing something obvious.

What about my second suggestion?

Regards,
Sven

Reply by email

Mark Morgan Lloyd
to Sven Barth
Sven Barth wrote 44 lines
On 27.01.2012 23:51, Mark Morgan Lloyd wrote: > Sven Barth wrote: >> On 27.01.2012 22:46, Mark Morgan Lloyd wrote: >>> If a class has a public reader property like this: >>> >>> TLexemeList= class(TObject) >>> protected >>> fValue: TPrimaevalValue; >>> public >>> property Value: TPrimaevalValue read fValue; >>> ... > > Note that below is in a different unit. > >>> is there a way of allowing a class helper to augment this with a >>> protected writer property? My attempt as below messed up the reader: >>> >>> TLLEvaluator = class helper for TLexemeList >>> private >>> procedure SetValue(v: TPrimaevalValue); >>> protected >>> property Value: TPrimaevalValue write SetValue; >>> ... >>> >> >> In your exact example fValue is protected so the following should work: >> >> property Value: TPrimaevalValue read fValue write SetValue; > > That didn't work since once we're in the helper it can't see fValue > since it's only visible in the original unit. > You are right that it does not work, but you're not right about the reason. Helpers are able to access published, public and protected members of its extended class (e.g. you're able to write "fValue := aValue" in your setter inside the helper), but you can't make use of fields and methods of the extended class in the helper's declaration. > Obviously code in the helper can simply call SetValue(), but I thought > it worth raising in case I was missing something obvious. > What about my second suggestion?

Sorry, it was getting late and I think I missed the GetValue() detail.

property Value: TPrimaevalValue read GetValue write SetValue;

..

function TLLEvaluator.GetValue: TPrimaevalValue;
begin
   Result := inherited Value;
end;

Thanks, I think that works but I need to do more testing.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email