Forum > General

A doubt about the visibility specifiers in the classes

<< < (2/3) > >>

MarkMLl:

--- Quote from: simone on July 26, 2022, 11:04:35 am ---Post Scriptum: It's obvious that the example does not show a good style of programming. I exemplified a situation that happened to me during a refactoring, in which certain edge situations like this can occur.

--- End quote ---

I think this is something that the compiler could usefully warn about, but is quite simply such an absurdity (in the strictly logical sense) that it can be forgiven for overlooking it.

Do you have a more concrete example?

MarkMLl

simone:
I have a more concrete example, but it comes from too large a code base to show here. In brief, as part of a refactoring process, I reorganized some classes and had an unexpected outcome that made me discover this behavior.

I just wanted to know if it was known and expected. For me, modestly, it was not an expected behavior. That's all.

kupferstecher:
Here is a simple test that might be related to the issue, see below. There are no classes involved, but a (record) type that is defined in an other unit (unit uTypes). The type is the result of a function implemented in Unit1. Obviously uTypes needs to be added to "uses" in that unit. Unit2 uses the function of unit1 and can access the result type even if uTypes is not added to "uses", see the code of unit2, especially the comments. I think its useful.

project1.lpr:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program project1;uses Unit1, uTypes, Unit2;begin  WriteSth;  ReadLn;end.  
unit1.pas:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1;{$mode ObjFPC}{$H+}INTERFACE uses uTypes; Function GetValue: TSth; IMPLEMENTATION Function GetValue:TSth;begin  Result.one:= 1;  Result.two:= 2;end; end.
unit2.pas:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit2;{$mode ObjFPC}{$H+}INTERFACE uses Unit1; Procedure writeSth; IMPLEMENTATION Procedure writeSth;//var res: TSth; //not possible, if uTypes is not in usesbegin  writeln('Value: ',GetValue.two); //record member can be accessed without uTypes in "uses"end; end.
uTypes.pas:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit uTypes;{$mode ObjFPC}{$H+}INTERFACE Type TSth = record  one: Integer;  two: Integer;end; IMPLEMENTATION end.

MarkMLl:

--- Quote from: kupferstecher on July 26, 2022, 12:17:24 pm ---Here is a simple test that might be related to the issue, see below. There are no classes involved, but a (record) type that is defined in an other unit (unit uTypes). The type is the result of a function implemented in Unit1. Obviously uTypes needs to be added to "uses" in that unit. Unit2 uses the function of unit1 and can access the result type even if uTypes is not added to "uses", see the code of unit2, especially the comments. I think its useful.

--- End quote ---

I'd suggest that there's a big difference between "needs to be added to 'uses'" and "is automatically made public" which is what OP was demonstrating.

MarkMLl

kupferstecher:

--- Quote from: MarkMLl on July 26, 2022, 12:39:20 pm ---I'd suggest that there's a big difference between "needs to be added to 'uses'" and "is automatically made public" which is what OP was demonstrating.
--- End quote ---

Agreed, but I don't see that the OP demonstarted that the type itself was automatically made public[1], but only that the enum identifiers were made public. And I see the enum identifiers similar to the record member identifiers.

Then it seems these identifiers are automatically made available by the compiler if the type is used as parameter. And "making it available" in the one case is like making it public and in the other case like beeing added to uses.

[1]
If you try to add
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var enum: TMyEnum; in project1.lpr then it will fail:
project1.lpr(6,9) Error: Identifier not found "TMyEnum"

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version