Recent

Author Topic: Identifyer visibility: program, subprogram, unit, class, private/public etc.  (Read 376 times)

Researching

  • Full Member
  • ***
  • Posts: 100
Visibility is convenient for avoiding bugs, debugging, readability of code etc.
But, what is the complete hierarchy?
  • - program
  • - subprogram,
  • - unit, 
  • - library(exported...), 
  • - class, 
  • - private/public/published/protected/ 
  • - etc.
// for example what will happen if two classes declared in the same unit?
// inherit from a class in library?
// inheritance through "exported" - is it possible?

Maybe all these things are described in documents, but did not find all stuff in the same place.

For example: made a code in two units:
first - main of program
second - unit with reusable subprograms
And could not make a proper typecast: same declaration in different units was considered as different types.
The solution was elementary: declare the  type in a separate unit and include it in <uses ....; > in both: first and second units.
But this took a couple of hours to find out.
And how many more combinations are possible?...

So could anyone explain the whole concept of FPC visibility of identifiers? 
« Last Edit: September 01, 2022, 10:20:13 am by Researching »

Thaddy

  • Hero Member
  • *****
  • Posts: 12208
« Last Edit: September 01, 2022, 10:27:44 am by Thaddy »
Manuals, manuals, manuals first.
You have incompetence and sheer incompetence.

Researching

  • Full Member
  • ***
  • Posts: 100
1. Strict private - you mean protected?

And that about RTTI - is one of things hardly found in docs. Very valuable answer :)

Thaddy

  • Hero Member
  • *****
  • Posts: 12208
Nope, see my above edit and the docs:
https://www.freepascal.org/docs-html/ref/refse35.html
That is about the same as I wrote before the edit.
« Last Edit: September 01, 2022, 10:30:33 am by Thaddy »
Manuals, manuals, manuals first.
You have incompetence and sheer incompetence.

Researching

  • Full Member
  • ***
  • Posts: 100
Oh, if could bring back that first version of your topic...
It was clear and simple for initial comprehension.
Have seen the docs on the link provided...
Docs are usually peculiarly detailed, and mostly require either big knowledge or lots of time to get on with.
... So could you please return the initial version of your post?

KodeZwerg

  • Hero Member
  • *****
  • Posts: 559
  • Fifty shades of code.
    • Delphi & FreePascal
Maybe it help you when you simply create a "Father" class and from that in a different unit a "Son" class, try having all possible visibilities included and try access them.
"Learning by doing..."

for units in general, everything between "interface" and "implementation" will be accessible from other units that "uses" that.
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

Thaddy

  • Hero Member
  • *****
  • Posts: 12208
Oh, if could bring back that first version of your topic...
Ok, but the manual is clear, I think. But here's the old version that I removed in my edit:
1. strict private : only accessible from the class itself, but not through inheritance.
2. private : only accessible from the unit where it is declared, also by other classes in the same unit but not through inheritance from other units
3. strict protected : only accessible by the class itself or through inheritance, inheritance can make it public or published (promote visibility)
4. protected : only accessible by inheritance or from within the same unit, inheritance can make it public or published (promote visibility)
5. public : accessible anywhere
6. published : accessible anywhere and has type information generated (RTTI)  so it can e.g. be used by the IDE ($M+}, RTTI has more applications btw.

This is more or less the same as the manual and I should have referred to the manual in the first place.
That is why I removed it and replaced it with the link to the manual.
« Last Edit: September 01, 2022, 02:58:36 pm by Thaddy »
Manuals, manuals, manuals first.
You have incompetence and sheer incompetence.

Researching

  • Full Member
  • ***
  • Posts: 100
Thank you, Thaddy.

 

TinyPortal © 2005-2018