Forum > General

A doubt about the visibility specifiers in the classes

(1/3) > >>

simone:
Consider the following program and the unit it uses:


--- 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;{$mode ObjFPC}{$H+}uses unit1;var  MyClass : TMyClass; begin  MyClass:=TMyClass.Create;  MyClass.Test(Value1);  MyClass.Free;end.

--- 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+} interfaceuses  Classes, SysUtils;type   TMyClass=class    private      type        TMyEnum=(Value1,Value2);    public      procedure Test(MyEnum : TMyEnum);  end; implementation procedure TMyClass.Test(MyEnum: TMyEnum);begin  writeln(MyEnum);end; end.
In the Unit1, TMyEnum is an enum type declared in private section of TMyClass, so it should only be visible in the module that contains the class definition.
Nevertheless in the main program I can use the values of TMyEnum as parameters to invoke the Test method of the same class.

This is also possible if strict private is used.

Why are the values of TMyEnum visible outside the module, even though they have been declared as (strict) private in the class they belong to?

Thanks in advance for the kind explanations.

marcov:
It is a bit contrived having a public method with a private type.  How do you imagine that this would work?

Can you access the values (e.g. ord()ing) without declaring that method? Maybe the method causes the enum to be exported.

simone:
I'm not saying this is good programming practice, but since it's allowed, I'd just like to understand why the compiler behaves this way.

Thaddy:
Because the public test method is an access specifier?
It would be different if the enum itself is accessible directly from other units without an access specifier or even with strict private from the same unit: in that case it is a bug. But it isn't. The method 'exports' it because of its parameter.

simone:
Thanks Thaddy. So should I assume that a public method of a class makes public the types of its parameters that have been declared private in the same class?

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.

Navigation

[0] Message Index

[#] Next page

Go to full version