Sign in Join

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

Mailing lists / fpc-devel / 2026-01

protected class section / documentation

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

Martin

According to https://www.freepascal.org/docs-html/ref/refse34.html

Protected Is the same as Private, except that the members of a Protected section are also accessible to descendent types, even if they are implemented in other modules.

"to descendant types" => suggests, from within a descendant type (even if the descendant type is in a different unit).

But the example (working with 3.3.1) from
https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/41987 shows that the protected method is accessible
*_through_* the descendant type.

I.e. from any code in the unit of the descendant type, even code outside the descendant type, if it goes "through" the descendant type.

Reply by email

Michael Van Canneyt
to Martin
Martin wrote 17 lines
According to https://www.freepascal.org/docs-html/ref/refse34.html > Protected > Is the same as Private, except that the members of a Protected > section are also accessible to descendent types, even if they are > implemented in other modules. "to descendant types" => suggests, from within a descendant type (even if the descendant type is in a different unit). But the example (working with 3.3.1) from https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/41987 shows that the protected method is accessible *_through_* the descendant type. I.e. from any code in the unit of the descendant type, even code outside the descendant type, if it goes "through" the descendant type.

What is your question ?

The behaviour is normal and logical:

The descendant type is subject to the scoping rules. It inherits the protected methods from its parent (with the same visibility) and a protected method is visible in the same unit/program.

The code that uses the descendent class does not know and has no way of knowing where the protected method originates from. It knows the descendent class and can use all protected methods of the descendent class, even if they come from a parent class.

That is why the 'strict' protected was introduced. If you change the visibility to strict private in the example, the program will no longer compile.

Michael.

Reply by email

Martin Frb
to Michael Van Canneyt
Michael Van Canneyt wrote 23 lines
On Wed, 7 Jan 2026, Martin via fpc-devel wrote: > According to https://www.freepascal.org/docs-html/ref/refse34.html > >> Protected >>     Is the same as Private, except that the members of a Protected >>     section are also accessible to descendent types, even if they are >>     implemented in other modules. > > "to descendant types" => suggests, from within a descendant type > (even if the descendant type is in a different unit). > > But the example (working with 3.3.1) from > https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/41987 > shows that the protected method is accessible > *_through_* the descendant type. > > I.e. from any code in the unit of the descendant type, even code > outside the descendant type, if it goes "through" the descendant type. What is your question ?

Should the doc be clearer on this?

- "to" in "to descendant types" from an English point of view says "the descendant themself" or "the code inside the descendant"
- "through" would be more meaningful here.
- "on" or "via" would also work

Reply by email

Michael Van Canneyt
to Martin Frb
Martin Frb wrote 29 lines
On 08/01/2026 10:01, Michael Van Canneyt via fpc-devel wrote: > > > On Wed, 7 Jan 2026, Martin via fpc-devel wrote: > >> According to https://www.freepascal.org/docs-html/ref/refse34.html >> >>> Protected >>>     Is the same as Private, except that the members of a Protected >>>     section are also accessible to descendent types, even if they are >>>     implemented in other modules. >> >> "to descendant types" => suggests, from within a descendant type >> (even if the descendant type is in a different unit). >> >> But the example (working with 3.3.1) from >> https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/41987 >> shows that the protected method is accessible >> *_through_* the descendant type. >> >> I.e. from any code in the unit of the descendant type, even code >> outside the descendant type, if it goes "through" the descendant type. > > What is your question ? Should the doc be clearer on this? - "to" in "to descendant types" from an English point of view says "the descendant themself" or "the code inside the descendant"

Yes, because that is what is meant.

What you observe is a corollary of the scoping rules as I explained above. I'll add a remark.

Michael.

Reply by email

Martin Frb
to Michael Van Canneyt
Michael Van Canneyt wrote 13 lines
On Thu, 8 Jan 2026, Martin Frb via fpc-devel wrote: > > Should the doc be clearer on this? > > - "to" in "to descendant types" from an English point of view says > "the descendant themself" or "the code inside the descendant" Yes, because that is what is meant. What you observe is a corollary of the scoping rules as I explained above. I'll add a remark.

And maybe take into account "module" vs "modules". I.e. while for "private" it always is just one single module, for "protected" the class (including its  bases) may be declared across several modules.

So that adds in the current text, to the misleading/omitting character of the doc.

Reply by email